1. 程式人生 > >Silver Cow Party

Silver Cow Party

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.

題解:

第一個數字代表有幾個N個農場,及M條路來連線這N個農場,及X表示舉辦party的農場求最短路的題,這個題主要是分為去和來兩個過程,這就需要我們用兩次Djikstra演算法,求兩次最小路,然後去加一下求去和的最大的,具體程式碼實現如下,反向回去的路要反向建圖,然後就達到反向的目的

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

const int INF=0x3f3f3f3f;
int p[1005][1005];
int dis1[1005];
int dis2[1005];
int vis[1005];
int main()
{
    int i,j,n,m,x,a,b,c,key,minn;
    scanf("%d%d%d",&n,&m,&x);
    for(i=1;i<=n;i++)
    {
        dis1[i]=INF;
        dis2[i]=INF;
        vis[i]=0;
        for(j=1;j<=n;j++)
            p[i][j]=INF;
    }

    for(i=0;i<m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        p[a][b]=c;
    }
    dis1[x]=0;
    key=x;
    for(i=1;i<=n;i++)
    {
        minn=INF;
        for(j=1;j<=n;j++)
        {
            if(!vis[j]&&dis1[j]<minn)
            {
                minn=dis1[j];
                key=j;
            }
        }
        vis[key]=1;
        for(j=1;j<=n;j++)
        {
            if(minn+p[key][j]<dis1[j])
                dis1[j]=minn+p[key][j];
        }
    }
    for(i=1;i<=n;i++)
    {
        vis[i]=0;
    }
    dis2[x]=0;
    key=x;
    for(i=1;i<=n;i++)
    {
        minn=INF;
        for(j=1;j<=n;j++)
        {
            if(!vis[j]&&dis2[j]<minn)
            {
                minn=dis2[j];
                key=j;
            }
        }
        vis[key]=1;
        for(j=1;j<=n;j++)
        {
            if(minn+p[j][key]<dis2[j])
                dis2[j]=minn+p[j][key];
        }
    }
    minn=0;
    for(i=1;i<=n;i++)
    {
        if(minn<dis1[i]+dis2[i])
            minn=dis1[i]+dis2[i];
    }
    printf("%d\n",minn);
    return 0;
}

相關推薦

Luogu P1821 [USACO07FEB]銀牛派對Silver Cow Party

ace first tar 思路 mes and test style gin P1821 [USACO07FEB]銀牛派對Silver Cow Party 題目描述 One cow from each of N farms (1 ≤ N ≤ 1000) c

洛谷 1821 [USACO07FEB]銀牛派對Silver Cow Party

ide 14. set using b- -- name class size 【題解】   其實解法 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm>

POJ 3268 Silver Cow Party

turn border can rate hat n) output conn stream Language: Default Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K To

[USACO07FEB]銀牛派對Silver Cow Party

swa silver 最短路徑 digi edge pop names queue this 題目描述 寒假到了,N頭牛都要去參加一場在編號為X(1≤X≤N)的牛的農場舉行的派對(1≤N≤1000),農場之間有M(1≤M≤100000)條有向路,每條路長Ti(1≤Ti≤10

POJ--3268 Silver Cow Party(最短路)

iostream {} push prior namespace sca author sil main 題目電波:POJ--3268 Silver Cow Party 跑兩遍 dijkstra 就好了 弱智題 #include<iostream&

Silver Cow Party (Dijkstra)

      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

POJ-3268 -D - Silver Cow Party(最短路)

題目描述: 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 ≤ X ≤ N). A tota

Silver Cow Party(兩次Dijstra)

Silver Cow Party 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 #

Silver Cow Party POJ

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 ≤ X

Silver Cow Party (定點X為起點和終點的最長距離)

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 ≤ X

【題解:POJ3268 Silver Cow Party】(最短路問題)

傳送門:(http://poj.org/problem?id=3268) 題目描述: 農場有N(1≤N≤1000)個牛棚,每個牛棚都有1只奶牛要參加在X牛棚舉行的奶牛派對.共有M(1≤M≤100000)條單向路連線著牛棚,第i條踣需要Ti的時間來通過.牛們都很懶,所以不管是前去X牛棚參加派對

Poj 3268 Silver Cow Party (最短路)

題意:一張N*N的有向圖,M條邊,每個點上都有一頭牛。給出一個X,表示牛的目的地。求出所有牛到達X的最短路。然後再求出所有牛回到自己原來位置的最短路。然後計算哪頭牛使用時間最長。 題解:兩遍dijkstra,搞定。詳情看程式碼註釋。 #include<cstdio&g

poj 3268 Silver Cow Party 【最短路,有向圖】

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18401 Accepted: 8421 Description One cow from each of N

Silver Cow Party

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 ≤ X ≤

poj3268 Silver Cow Party【最短路】

題目連結:http://poj.org/problem?id=3268 題意:有n個農場,在x號農場有一個派對,農場之間有m條單向邊連線著這些農場,每條邊有一定的時間花費,每隻牛都會從自己農場出發去參

poj 3268 Silver Cow Party 最短路/dijkstra

#include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <algorithm> #include <i

POJ 3268 Silver Cow Party(雙向最短路)

Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow p

Dijkstra演算法模板(POJ 3268)很好的理解題 Silver Cow Party

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29507 Accepted: 13395 Description One cow from ea

POJ-3268-Silver Cow Party(迪傑斯特拉 多點到star和star到多點)

D - Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice P

poj 3268 Silver Cow Party 題解

大意:有n個牧場,編號1到n,每個牧場有一頭奶牛。現在所有奶牛要到編號為x的牧場聚會,路徑是單向的,奶牛都很聰明,只走最短路徑,問哪頭奶牛來回走的路徑之和最大,輸出這個最大值。 思路:建立兩個鄰接表(一個出邊表,一個入邊表),然後分別對兩個鄰接表使用一次SPFA,得到的路