1 条题解
-
0
C++ :
#include<cstdio> #include<queue> #include<cstring> using namespace std; queue<int> q; int rotb1[1001][1001],rotr1[1001][1001]; int rotb2[1001][1001],rotr2[1001][1001]; long long mi1[1002],mi2[1002]; bool d1[1001],d2[1001]; int main() { //freopen("party.in","r",stdin); //freopen("party.out","w",stdout); int n,m,k,a,b,r; long long maxx=0; scanf("%d%d%d",&n,&m,&k); for (int i=1;i<=m;i++) { scanf("%d%d%d",&b,&a,&r); rotb1[a][++rotb1[a][0]]=b; rotr1[a][++rotr1[a][0]]=r; rotb2[b][++rotb2[b][0]]=a; rotr2[b][++rotr2[b][0]]=r; } memset(mi1,127,sizeof(mi1)); memset(mi2,127,sizeof(mi2)); mi1[k]=mi2[k]=0; q.push(k); while (!q.empty()) { int num=q.front(); q.pop(); d1[num]=false; for (int i=1;i<=rotb1[num][0];i++) { b=rotb1[num][i]; r=rotr1[num][i]; if (mi1[b]>mi1[num]+r) { mi1[b]=mi1[num]+r; if(!d1[b]) { q.push(b); d1[b]=true; } } } } q.push(k); while (!q.empty()) { int num=q.front(); q.pop(); d2[num]=false; for (int i=1;i<=rotb2[num][0];i++) { b=rotb2[num][i]; r=rotr2[num][i]; if (mi2[b]>mi2[num]+r) { mi2[b]=mi2[num]+r; if(!d2[b]) { q.push(b); d2[b]=true; } } } } for (int i=1;i<=n;i++) if(maxx<mi1[i]+mi2[i]&&mi1[i]!=mi1[1001]&&mi2[i]!=mi2[1001]) maxx=mi1[i]+mi2[i]; printf("%lld",maxx); return 0; }
Pascal :
program party; type edge=record x,w,next:longint; end; var e,e2:array[0..200000]of edge; k,k2:array[0..2000]of longint; v:array[0..2000]of boolean; d,d2:array[0..2000]of longint; f:array[0..1000000]of longint; i,j,m,n,kk,x,y,z,ans:longint; tot,tot2:longint; procedure add(x,y,z:longint); begin inc(tot); e[tot].x:=y; e[tot].w:=z; e[tot].next:=k[x]; k[x]:=tot; end; procedure add2(x,y,z:longint); begin inc(tot2); e2[tot2].x:=y; e2[tot2].w:=z; e2[tot2].next:=k2[x]; k2[x]:=tot2; end; procedure SPFA1; var head,tail,t,x:longint; begin head:=0; tail:=1; f[tail]:=kk; fillchar(v,sizeof(v),0); fillchar(d,sizeof(d),63); d[kk]:=0; v[kk]:=true; while (head<tail) do begin inc(head); x:=f[head]; v[x]:=false; t:=k[x]; while (t<>0) do begin if (d[e[t].x]>d[x]+e[t].w) then begin d[e[t].x]:=d[x]+e[t].w; if (not v[e[t].x]) then begin v[e[t].x]:=true; inc(tail); f[tail]:=e[t].x; end; end; t:=e[t].next; end; end; end; procedure SPFA2; var head,tail,t,x:longint; begin head:=0; tail:=1; f[tail]:=kk; fillchar(v,sizeof(v),0); fillchar(d2,sizeof(d2),63); d2[kk]:=0; v[kk]:=true; while (head<tail) do begin inc(head); x:=f[head]; v[x]:=false; t:=k2[x]; while (t<>0) do begin if (d2[e2[t].x]>d2[x]+e2[t].w) then begin d2[e2[t].x]:=d2[x]+e2[t].w; if (not v[e2[t].x]) then begin v[e2[t].x]:=true; inc(tail); f[tail]:=e2[t].x; end; end; t:=e2[t].next; end; end; end; begin readln(n,m,kk); for i:=1 to m do begin readln(x,y,z); add(x,y,z); add2(y,x,z); end; SPFA1; SPFA2; ans:=0; for i:=1 to n do if (i<>kk)and(d[i]<1000000)and(d2[i]<1000000) then begin if (d[i]+d2[i]>ans) then ans:=d[i]+d2[i]; end; writeln(ans); end.
- 1
信息
- ID
- 991
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者