Poj 3268(Dijkstra)

來源:互聯網
上載者:User
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 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
先在原圖上求一次單源最短路,再把所有邊反向,再求一次,把距離累加即可。
#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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.