Topic Portal
The daily water wave, is to the positive diagram run over SPFA, and then build a reverse graph, that is, all the other nodes to the source point of distance, and then run again SPFA.
The final output is two times the maximum value of the distance is OK.
Attach the AC code:
#include <cstdio> #include <cctype> #include <queue> #include <cstring> #define M 100010 #define N
1010 using namespace Std; struct note{int to,w,nt;}
Z[M*2],F[M*2];
int n,m,st,num,hz[n],hf[n],x,y,w,d1[n],d2[n];
Long long ans;
Queue <int> que;
BOOL B[n];
void Read (int& a) {static char C=getchar (); A=0;int f=1;
while (!isdigit (c)) {if (c== '-') F=-1;c=getchar ();}
while (IsDigit (c)) a=a*10+c-' 0 ', C=getchar ();
A*=f;return;
} void Add (int x,int y,int W) {z[num]= (note) {X,w,hz[y]};
f[num]= (note) {y,w,hf[x]};
hz[y]=hf[x]=num++;
} void Spfa1 () {while (!que.empty ())) Que.pop ();
memset (b,0,sizeof b);
memset (d1,0x3f,sizeof D1);
Que.push (ST), d1[st]=0,b[st]=1;
while (!que.empty ()) {int P=que.front (); Que.pop (), b[p]=0;
for (int i=hz[p]; ~i; i=z[i].nt) if (D1[Z[I].TO]>D1[P]+Z[I].W) {D1[Z[I].TO]=D1[P]+Z[I].W;
if (!b[z[i].to]) {b[z[i].to]=1;
Que.push (z[i].to);
}}} return; } void Spfa2 () {while (!que.empty ()) Que.pop ();
memset (b,0,sizeof b);
memset (d2,0x3f,sizeof D2);
Que.push (ST), d2[st]=0,b[st]=1;
while (!que.empty ()) {int P=que.front (); Que.pop (), b[p]=0;
for (int i=hf[p]; ~i; i=f[i].nt) if (D2[F[I].TO]>D2[P]+F[I].W) {D2[F[I].TO]=D2[P]+F[I].W;
if (!b[f[i].to]) {b[f[i].to]=1;
Que.push (f[i].to);
}}} return;
} int main (void) {read (n), read (m), read (ST);
memset (hz,-1,sizeof Hz), memset (hf,-1,sizeof HF);
for (int i=1; i<=m; ++i) Read (x), read (y), read (W), add (X,Y,W);
SPFA1 (), SPFA2 ();
for (int i=1; i<=n; ++i) Ans=max (ans, (long Long) d1[i]+d2[i]);
printf ("%lld", ans);
return 0; }