码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3268 Silver Cow Party

时间:2014-08-02 18:29:13      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   strong   io   for   


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; 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 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


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

#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;
}

POJ 3268 Silver Cow Party,布布扣,bubuko.com

POJ 3268 Silver Cow Party

标签:des   style   http   color   os   strong   io   for   

原文地址:http://blog.csdn.net/kimi_r_17/article/details/38349151

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!