Silver Cow Party POJ - 3268
考察:最短路
错误思路:
floyd,肉眼可见的超时(但是有用floyd过的,tql)
正确思路:
spfa+两次建图
正向建一次反向建一次,这样就是单源最短路
1 #include <iostream> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 const int N = 1010; 6 int n,m,x,g[N][N],dist[N],st[N],maxn[N],ans; 7 struct Edge{ 8 int s,e,w; 9 }edge[100010]; 10 void spfa(int s) 11 { 12 memset(dist,0x3f,sizeof dist); 13 memset(st,0,sizeof st); 14 dist[s] = 0; 15 queue<int> q; 16 q.push(s); 17 st[s] = 1; 18 while(q.size()) 19 { 20 int t = q.front(); 21 q.pop(); 22 st[t] = 0; 23 for(int i=1;i<=n;i++) 24 { 25 if(dist[i]>dist[t]+g[t][i]) 26 { 27 dist[i] = dist[t]+g[t][i]; 28 if(!st[i]) q.push(i),st[i] = 1; 29 } 30 } 31 } 32 for(int i=1;i<=n;i++) maxn[i] += dist[i]; 33 } 34 int main() 35 { 36 // freopen("in.txt","r",stdin); 37 scanf("%d%d%d",&n,&m,&x); 38 for(int i=1;i<=n;i++) 39 for(int j=1;j<=n;j++) 40 if(i==j) g[i][j] = 0; 41 else g[i][j] = 0x3f3f3f3f; 42 for(int i=1;i<=m;i++) 43 { 44 int s,e,w; scanf("%d%d%d",&s,&e,&w); 45 g[e][s] = min(w,g[e][s]); 46 edge[i].s = s,edge[i].e = e,edge[i].w = w; 47 } 48 spfa(x); 49 for(int i=1;i<=n;i++) 50 for(int j=1;j<=n;j++) 51 if(i==j) g[i][j] = 0; 52 else g[i][j] = 0x3f3f3f3f; 53 for(int i=1;i<=m;i++) g[edge[i].s][edge[i].e] = min(edge[i].w,g[edge[i].s][edge[i].e]); 54 spfa(x); 55 for(int i=1;i<=n;i++) ans = max(ans,maxn[i]); 56 printf("%d\n",ans); 57 return 0; 58 }