poj3268Silver Cow Party(单源最短路径)

来源:互联网 发布:u盘数据丢失 编辑:程序博客网 时间:2024/06/05 19:16

题意:

n个牛要到牛x处聚会,聚会完后回到自己的地方,每个牛走的都是最短路,问哪一个牛走的路用的时间最长。方法:以牛x为起点,通过dijiktra算法就可以求出每个牛的返程路径,但每个牛去牛x的路径不可能用floyd算法,所有我们把所有路径返过来,再以牛x为起点,求一次最短路径,就可以知道每个牛的去程时间了。

Silver Cow PartyTime Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu

SubmitStatusPracticePOJ 3268

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, and Ti. The described road runs from farmAi 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.
AC代码

#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<queue>#include<map>#include<set>#define INF 999999999#define LL long long#define mod 1000003using namespace std;struct Edge{int to, dist;Edge(){}Edge(int v, int d) :to(v), dist(d) {}};struct node{    int from,to,dist;};node road[100005];//记录路径,之后反过来int n, m;vector<Edge> edges[1005];bool done[1005];int d[1005];void init(){for (int i = 0; i <= n; i++)edges[i].clear();}struct HeapNode{int d,u;HeapNode(int dd,int uu){    d=dd;    u=uu;}bool operator < (const HeapNode& rhs) const {return d>rhs.d;}};void dijk(int s){    for(int i=1;i<=n;i++)        d[i]=INF;    d[s]=0;priority_queue<HeapNode>q;    memset(done, 0, sizeof(done));q.push(HeapNode( 0,s ));while (!q.empty()){HeapNode x = q.top();q.pop();int u = x.u;if (done[u]) continue;done[u] = true;for (int i = 0; i<edges[u].size(); i++){Edge& e = edges[u][i];if(d[e.to]>d[u]+e.dist)            {                d[e.to]=d[u]+e.dist;                q.push(HeapNode(d[e.to],e.to));            }}}}int main(){    int mm;    int x;    while(scanf("%d%d%d",&n,&mm,&x)!=EOF){    int a, b, c;    init();    int dd[1005];    int sum=-1;    for (int i = 0; i<mm; i++)                {                    scanf("%d %d %d", &a, &b, &c);                    road[i].from=a,road[i].to=b,road[i].dist=c;                    edges[a].push_back(Edge(b,c));                }            dijk(x);            for(int i=1;i<=n;i++)                dd[i]=d[i];            init();//记得再次初始化            for(int i=0;i<mm;i++)            {                a=road[i].from,b=road[i].to,c=road[i].dist;                edges[b].push_back(Edge(a,c));            }            dijk(x);            for(int i=1;i<=n;i++)                {                    dd[i]+=d[i];                    if(dd[i]>sum)                        sum=dd[i];                }            printf("%d\n",sum);}}


0 0