POJ3258——Silver Cow Party

来源:互联网 发布:安卓一键改串号软件 编辑:程序博客网 时间:2024/04/30 01:39

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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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.

模板题,不多解释了,用的是spfa。
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<iterator>#include<vector>#include<queue>#include<list>#include<stack>#include<map>#include<set>#define inf 0x3f3f3f3fusing namespace std;const int maxn=100010;struct node{int to;int weight;int next;}edge1[100010],edge2[100010];int head1[maxn],head2[maxn];int tot1,tot2;void addedge1(int from,int to,int weight){edge1[tot1].to=to;edge1[tot1].weight=weight;edge1[tot1].next=head1[from];head1[from]=tot1++;}void addedge2( int from,int to,int weight){edge2[tot2].to=to;edge2[tot2].weight=weight;edge2[tot2].next=head2[from];head2[from]=tot2++;}int dist1[1010],dist2[1010];void spfa_1(int n,int v){memset(dist1,inf,sizeof(dist1));queue<int>qu;dist1[v]=0;while(!qu.empty())  qu.pop();    qu.push(v);    while(!qu.empty())    {    int cur=qu.front();    qu.pop();    for(int i=head1[cur];i!=-1;i=edge1[i].next)    {    if(dist1[edge1[i].to] > dist1[cur]+edge1[i].weight)    {    dist1[edge1[i].to] = dist1[cur]+edge1[i].weight;    qu.push(edge1[i].to);     }    }    }}void spfa_2(int n,int v){memset(dist2,inf,sizeof(dist1));queue<int>qu;dist2[v]=0;while(!qu.empty())  qu.pop();    qu.push(v);    while(!qu.empty())    {    int cur=qu.front();    qu.pop();    for(int i=head2[cur];i!=-1;i=edge2[i].next)    {    if(dist2[edge2[i].to] > dist2[cur]+edge2[i].weight)    {    dist2[edge2[i].to] = dist2[cur]+edge2[i].weight;        qu.push(edge2[i].to);    }    }    }}int main(){int n,m,x;while(~scanf("%d%d%d",&n,&m,&x)){memset(head1,-1,sizeof(head1));memset(head2,-1,sizeof(head2));tot1=0;tot2=0;int s,t,w;while(m--){scanf("%d%d%d",&s,&t,&w);addedge1(s,t,w);addedge2(t,s,w);}int maxs=0;spfa_1(n,x);spfa_2(n,x);for(int i=1;i<=n;i++)  maxs=max(maxs,dist1[i]+dist2[i]);        printf("%d\n",maxs);}return 0;}


0 0
原创粉丝点击