Silver Cow Party
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14370 | Accepted: 6480 |
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
Source
题意:现在有N头牛,编号为1~N,分别住在N个不同的地方,这N个地方组成的图有M条单向的路。
现在编号为X的牛要组织一场盛大的party,邀请所有的牛去他家玩。
每头牛要先去X的家,然后再回自己的家,牛总是走可以走的最短的路。
问:哪一头牛来回所花费的时间最多,输出最多的花费时间。
直接dijkstra
先dijkstra一次
然后对边进行一次反转,即把所有的有向边的方向反转
再dijkstra一次
然后找出dis[0][i]+dis[1][i]中的最大值即可。
好,睡觉。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 5 using namespace std; 6 7 const int MAXN=1005; 8 const int INF=0x3f3f3f3f; 9 10 int cost[MAXN][MAXN]; 11 int dis[2][MAXN]; 12 bool vis[MAXN]; 13 bool vis_two[MAXN][MAXN]; 14 15 void dijkstra(int cnt,int N,int X) 16 { 17 for(int i=1;i<=N;i++) 18 dis[cnt][i]=INF; 19 for(int i=1;i<=N;i++) 20 vis[i]=false; 21 dis[cnt][X]=0; 22 23 for(int j=1;j<=N;j++) 24 { 25 int minc=INF; 26 int p=-1; 27 for(int i=1;i<=N;i++) 28 { 29 if(!vis[i]&&dis[cnt][i]<minc) 30 { 31 minc=dis[cnt][i]; 32 p=i; 33 } 34 } 35 vis[p]=true; 36 for(int i=1;i<=N;i++) 37 { 38 if(!vis[i]&&dis[cnt][p]+cost[p][i]<dis[cnt][i]) 39 dis[cnt][i]=dis[cnt][p]+cost[p][i]; 40 } 41 } 42 } 43 44 void change(int N) 45 { 46 memset(vis_two,false,sizeof(vis_two)); 47 for(int i=1;i<=N;i++) 48 { 49 for(int j=1;j<=N;j++) 50 { 51 if(!vis_two[i][j]) 52 { 53 int tmp=cost[i][j]; 54 cost[i][j]=cost[j][i]; 55 cost[j][i]=tmp; 56 vis_two[i][j]=true; 57 vis_two[j][i]=true; 58 } 59 } 60 } 61 } 62 63 void solve(int N) 64 { 65 int ret=-1; 66 for(int i=1;i<=N;i++) 67 { 68 if(dis[0][i]+dis[1][i]>ret) 69 ret=dis[0][i]+dis[1][i]; 70 } 71 printf("%d ",ret); 72 } 73 74 int main() 75 { 76 int N,M,X; 77 while(~scanf("%d%d%d",&N,&M,&X)) 78 { 79 for(int i=1;i<=N;i++) 80 { 81 for(int j=1;j<=N;j++) 82 { 83 if(i==j) 84 cost[i][j]=0; 85 else 86 cost[i][j]=INF; 87 } 88 } 89 for(int i=0;i<M;i++) 90 { 91 int u,v,w; 92 scanf("%d%d%d",&u,&v,&w); 93 cost[u][v]=w; 94 } 95 96 dijkstra(0,N,X); 97 change(N); 98 dijkstra(1,N,X); 99 100 solve(N); 101 } 102 return 0; 103 }