【POJ】 3268 Silver Cow Party

来源:互联网 发布:采集电脑数据 编辑:程序博客网 时间:2024/05/21 21:43
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16290 Accepted: 7460

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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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.


题解:这一题就是做两次单元最短路径,不同的就是把所有边反向就好。

#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;struct Edge{    int from,to,d,next;};struct Edge e[100005];int n,l,m,x,ans[1005],d[1005],vis[1005],pos[1005];queue<int> q;int spfa(int s){    for (int i=1;i<=n;i++) d[i]=10000000;    d[s]=0;    int x;    q.push(s); vis[s]=1;    while (!q.empty())    {        x=q.front();  q.pop(); vis[x]=0;        for (int i=pos[x];i!=0;i=e[i].next)            if (d[e[i].to]>d[e[i].from]+e[i].d)            {                d[e[i].to]=d[e[i].from]+e[i].d;                if (!vis[e[i].to])                {                    vis[e[i].to]=1;                    q.push(e[i].to);                }            }    }    return 1;}void reset(){    memset(pos,0,sizeof(pos));    for (int i=1;i<=m;i++)    {        int x;        x=e[i].from; e[i].from=e[i].to; e[i].to=x;        e[i].next=pos[e[i].from]; pos[e[i].from]=i;    }}int main(){    scanf("%d%d%d",&n,&m,&x);    for (int i=1;i<=m;i++)    {        scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].d);        e[i].next=pos[e[i].from]; pos[e[i].from]=i;    }    spfa(x);    for (int i=1;i<=n;i++) ans[i]+=d[i];    reset();    spfa(x);    for (int i=1;i<=n;i++) ans[i]+=d[i];    for (int i=1;i<=n;i++) l=max(ans[i],l);    printf("%d",l);    return 0;}


0 0
原创粉丝点击