Silver Cow Party
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?
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.
Output
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.
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 3Sample Output
10
题目大意:
有n个农场,m条路,在x农场开派对,每个农场都要有牛去x农场,每头牛来回都走最快的路,求来回用时最长的牛的时间。
从农场a到农场b用时为t。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> #define N 1005 #define inf 0x3f3f3f using namespace std; int n,m,x; int e[N][N],dis[N],vis[N],dback[N]; void dij() { for(int i=1;i<=n;i++) { vis[i]=0; dis[i]=e[x][i]; dback[i]=e[i][x]; } vis[x]=1; int minn,k; for(int i=1;i<n;i++) { minn=inf; for(int j=1;j<=n;j++) if(!vis[j]&&dis[j]<minn) { k=j; minn=dis[j]; } vis[k]=1; for(int j=1;j<=n;j++) if(dis[j]>dis[k]+e[k][j]) dis[j]=dis[k]+e[k][j]; } memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { minn=inf; for(int j=1;j<=n;j++) if(!vis[j]&&dback[j]<minn) { k=j; minn=dback[j]; } vis[k]=1; for(int j=1;j<=n;j++) if(dback[j]>dback[k]+e[j][k]) dback[j]=dback[k]+e[j][k]; } } int main() { while(~scanf("%d%d%d",&n,&m,&x)) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i==j) e[i][j]=0; else e[i][j]=inf; for(int i=1;i<=m;i++) { int a,b,t; scanf("%d%d%d",&a,&b,&t); e[a][b]=t; } dij(); int ans=-1; for(int i=1;i<=n;i++) if(dis[i]+dback[i]>ans) ans=dis[i]+dback[i]; printf("%d\n",ans); } return 0; }