现在的位置: 首页 > 综合 > 正文

Poj 3268(Dijkstra)

2013年06月10日 ⁄ 综合 ⁄ 共 2207字 ⁄ 字号 评论关闭
Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10589   Accepted: 4690

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

USACO 2007 February Silver
先在原图上求一次单源最短路,再把所有边反向,再求一次,把距离累加即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int maxn = 100000 + 5;
const int INF = 1000000000;
typedef long long LL;
typedef pair<int,int> P;

int n,m,x;
struct Edge{
    int from,to;
    LL dis;
}e[maxn];

LL ans[maxn];
LL d[maxn];
int vis[maxn];
vector<Edge> G[maxn];

void Dij(int x){
    priority_queue<P,vector<P>,greater<P> > Q;
    memset(vis,0,sizeof(vis));
    for(int i = 0;i <= n;i++) d[i] = INF;
    Q.push(P(0,x));
    while(!Q.empty()){
        P p = Q.top();Q.pop();
        int id = p.second;
        LL dis = p.first;
        if(vis[id] == 1) continue;
        vis[id] = 1;
        d[id] = dis;
        for(int i = 0;i < G[id].size();i++){
            Edge edgs = G[id][i];
            int to = edgs.to;
            LL der = edgs.dis;
            if(d[to] > d[id] + der){
                d[to] = d[id] + der;
                Q.push(P(d[to],to));
            }
        }
    }
}

int main(){
    while(scanf("%d%d%d",&n,&m,&x) != EOF){
        for(int i = 0;i <= n;i++) G[i].clear();
        for(int i = 0;i < m;i++){
            scanf("%d%d%I64d",&e[i].from,&e[i].to,&e[i].dis);
            G[e[i].from].push_back(e[i]);
        }
        Dij(x);
        for(int i = 0;i <= n;i++) ans[i] = d[i];
        for(int i = 0;i <= n;i++) G[i].clear();
        for(int i = 0;i < m;i++){
            swap(e[i].to,e[i].from);
            G[e[i].from].push_back(e[i]);
        }
        Dij(x);
        LL Max = 0;
        for(int i = 1;i <= n;i++){
            ans[i] += d[i];
            if(i != x){
                Max = max(Max,ans[i]);
            }
        }
        printf("%d\n",Max);
    }
    return 0;
}

抱歉!评论已关闭.