poj 3268 Silver Cow Party

来源:互联网 发布:好的爸爸网络用语 编辑:程序博客网 时间:2024/04/30 11:18
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10102 Accepted: 4489

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.

Source

USACO 2007 February Silver


最开始想的就是弗洛伊德。果断超时了。没办法。弗洛伊德虽简单但白白多算了很多不需要的数据。后面发现其实用两个adijk就行了。因为是有向路。所以两个adijk把路换下方向就行了。据说这叫矩阵倒置的思想。
ac代码
#include <stdio.h>#include<string.h>int INF=10000000;int n,maps1[1010][1010],dis1[1010],maps2[1010][1010],dis2[1010];void adijk(int maps[][1010],int *dis)//因为都是一样的算法。所以传参数处理了{    int i,j,p,mi,visit[1010];    memset(visit,0,sizeof visit);    for(i=1; i<=n; i++)    {        mi=INF;        p=-1;        for(j=1; j<=n; j++)            if(!visit[j]&&dis[j]<=mi)                mi=dis[j],p=j;        if(p==-1)//如果没找到最小值直接返回            return ;        visit[p]=1;        for(j=1; j<=n; j++)            if(!visit[j]&&dis[p]+maps[p][j]<dis[j])                dis[j]=dis[p]+maps[p][j];    }}int main(){    int m,x,i,j,a,b,t,ma;    scanf("%d%d%d",&n,&m,&x);    for(i=1; i<=n; i++)    {        dis1[i]=dis2[i]=INF;        for(j=1; j<=n; j++)        {            if(i==j)                maps1[i][j]=maps2[i][j]=0;            else                maps1[i][j]=maps2[i][j]=INF;        }    }    for(i=1; i<=m; i++)    {        scanf("%d%d%d",&a,&b,&t);        if(t<maps1[a][b])            maps1[a][b]=maps2[b][a]=t;//分别记录道路信息    }    dis1[x]=dis2[x]=0;    adijk(maps1,dis1);    adijk(maps2,dis2);    ma=-1;    for(i=1; i<=n; i++)    {        if(dis1[i]+dis2[i]>ma)            ma=dis1[i]+dis2[i];    }    printf("%d\n",ma);    return 0;}


原创粉丝点击