Silver Cow Party
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15533 | Accepted: 7045 |
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.
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.
题意大概是:有1-N头牛住在不同的地方,现在所有的牛要去参加银牛party,地点在X
有M条路(单向的)所有的牛都很懒,都要走最短的路去,最短的路回,求走的路最长的那头牛走的路程!
这个题挺好的!
要反向建图求去时的路程,正向建图求回来的路程!
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define INF 0x3f3f3f3f #define MAXN 1000+10 #define MAXM 200000+10 using namespace std; int head[MAXN],vis[MAXN],dis[MAXN]; int n,m,x; struct node { int from,to,weight,next; } edge[MAXM]; void input() { int i,a,c,b; memset(head,-1,sizeof(head)); for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c);//反向建图 //edge[i]={b,a,c,head[b]};//这种写法在poj过不了!害我ce两次 edge[i].from=b; edge[i].to=a; edge[i].weight=c; edge[i].next=head[b]; head[b]=i; } } void spfa() { memset(dis,INF,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[x]=0; vis[x]=1; queue<int> q; q.push(x); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int k=head[u];k!=-1;k=edge[k].next) { int v=edge[k].to; if(dis[v]>dis[u]+edge[k].weight) { dis[v]=dis[u]+edge[k].weight; if(!vis[v]) { vis[v]=1; q.push(v); } } } } } int main() { int a[MAXN],b[MAXN]; while(scanf("%d%d%d",&n,&m,&x)!=EOF) { int i; input(); spfa(); for( i=1;i<=n;i++) //printf("++%d ",dis[i]); a[i]=dis[i]; int t; memset(head,-1,sizeof(head)); for(i=0;i<m;i++)//正向建图 { t=edge[i].from; edge[i].from=edge[i].to; edge[i].to=t; edge[i].next=head[edge[i].from]; head[edge[i].from]=i; } spfa(); for( i=1;i<=n;i++) a[i]+=dis[i];//往返路程相加 int m=-1; for(i=1;i<=n;i++) m=m < a[i] ? a[i]:m;//求出最大的路程 printf("%d\n",m); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。