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
Output
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
#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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Silver Cow Party--poj3268(SPFA)
原文地址:http://blog.csdn.net/nvliba/article/details/47804813