首页 > 代码库 > POJ 3268 Silver Cow Party

POJ 3268 Silver Cow Party


Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12633 Accepted: 5631

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 ≤XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi 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, andTi. The described road runs from farmAi to farm Bi, requiringTi 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.

Source

USACO 2007 February Silver

题意:路是单向的,来回不能走一样的路,找每个牛需要的最短时间的最大值。
思路:用两次最短路算法,第二次将矩阵倒置。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdlib.h>

using namespace std;

int mp[1005][1005];
int dis[1005];
bool vis[1005];
int dis2[1005];

const int INF=100000009;
int n;
void Dijstra(int v){
    int minx,x;
    for(int i=0;i<=n;i++){
        dis[i]=mp[v][i];
        vis[i]=0;
    }
    vis[v]=1;
    dis[v]=0;
    for(int i=1;i<n;i++){
        minx=INF;
        for(int j=1;j<=n;j++){
            if(minx>dis[j]&&!vis[j]){
                minx=dis[j];
                x=j;
            }
        }
        if(minx==INF) break;
        vis[x]=1;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&dis[x]+mp[x][j]<dis[j])
                dis[j]=dis[x]+mp[x][j];
        }
    }
}
void Dijstra2(int v){
    int minx,x;
    for(int i=0;i<=n;i++){
        dis2[i]=mp[i][v];
        vis[i]=0;
    }
    vis[v]=1;
    dis2[v]=0;
    for(int i=1;i<n;i++){
        minx=INF;
        for(int j=1;j<=n;j++){
            if(minx>dis2[j]&&!vis[j]){
                minx=dis2[j];
                x=j;
            }
        }
        if(minx==INF) break;
        vis[x]=1;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&dis2[x]+mp[j][x]<dis2[j])
                dis2[j]=dis2[x]+mp[j][x];
        }
    }
}
int main(){
    int m,x;
    scanf("%d%d%d",&n,&m,&x);
    memset(mp,INF,sizeof(mp));
    for(int i=0;i<m;i++){
        int s,e,t;
        scanf("%d%d%d",&s,&e,&t);
        if(mp[s][e]>t)
            mp[s][e]=t;
    }
    Dijstra(x);
    Dijstra2(x);
    int ans=-INF;
    for(int i=1;i<=n;i++){
        if(i==x) continue;
        if(dis2[i]==INF||dis[i]==INF) continue;
        if(ans<dis[i]+dis2[i]) ans=dis2[i]+dis[i];
    }
    printf("%d\n",ans);
    return 0;
}