Poj3268--silver Cow Party (shortest way +GODV force)

Source: Internet
Author: User

Description

One cow from each of N farms (1≤n≤1000) conveniently numbered 1..N are going to attend the big Cow party to being held at Farm #X (1≤x≤n). A total of M (1≤m≤100,000) unidirectional (one-way roads connects pairs of farms; Road I requires Ti (1≤ti≤100) UN Its of time to traverse.

Each of the cow must walk to the "party" and "when the" is "over" return to her farm. Each cow is a lazy and thus picks an optimal route with the shortest time. A Cow ' s return route might is different from her original route to the party since roads is one-way.

Of all the cows, what's 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 the 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

At first I was counted as the shortest circuit between two points, so that each point will be used two times Dijkstra, loved the tle. Turn over the puzzle to know, Di is actually a single source from the beginning to all points of the shortest, and all the cattle to the target point is a multiple starting point to an end, so here to use the back of the patriarch's power, the adjacency matrix of the two subscript interchange on the line, so up to two times Dijlstra.

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 1010#define MOD 1000000007using namespace STD;int Map[1005][1005],cost[1005][1005],vis[1005],dis[1005],undis[1005];intN,m,x;intDijkstra () {intI,j,min,v;memset(Vis,0,sizeof(VIS)); for(i=1; i<=n; ++i) {dis[i]=Map[x]        [i]; undis[i]=MapI    [x]; } vis[x]=1; for(i=1; i<=n; ++i) {min=inf; for(j=1; j<=n; ++J) {if(!vis[j]&&dis[j]<min)                {MIN=DIS[J];            V=j; }} vis[v]=1; for(j=1; j<=n; ++J) {if(!vis[j]&&Map[V] [J]<inf) {if(dis[j]>dis[v]+Map[V] [j]) {dis[j]=dis[v]+Map[V]                [j]; }            }        }    }memset(Vis,0,sizeof(VIS)); vis[x]=1; for(i=1; i<=n; ++i) {min=inf; for(j=1; j<=n; ++J) {if(!vis[j]&&undis[j]<min)                {MIN=UNDIS[J];            V=j; }} vis[v]=1; for(j=1; j<=n; ++J) {if(!vis[j]&&Map[j] [V]<inf) {if(undis[j]>undis[v]+Map[j] [v]) {undis[j]=undis[v]+Map[j]                [v]; }            }        }    }intans=-1; for(intI=1; i<=n; ++i) {if(i!=x) {intTmp=dis[i]+undis[i];        Ans=max (Tmp,ans); }    }returnAns;}intMain () {scanf("%d%d%d", &n,&m,&x);intA,b,t; for(intI=1; i<=n; ++i) for(intj=1; j<=n; ++J)if(I!=J)MapI [J]=inf;Else                MapI [j]=0; while(m--) {scanf("%d%d%d", &a,&b,&t);MapA    [B]=t; }printf("%d\n", Dijkstra ());return 0;}

Poj3268--silver Cow Party (shortest way +GODV force)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.