poj3268Silver Cow Party——最短路变形

来源:互联网 发布:三观尽毁的淘宝评论 编辑:程序博客网 时间:2024/06/05 04:17

poj3268Silver Cow Party:http://poj.org/problem?id=3268

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16780 Accepted: 7663

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 ≤ 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) 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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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

题意:有n个农场,共有m条单源路。告诉你x号农场举办party,这n个农场里的牛都要去参加,参加完还要返回各自的农场(去和返不一定是同一条路),每条牛都走最短路,求这些牛中所走的路中最长的一条。

思路:先将去的时候的各自最短路求出来,然后将回来的时候的最短路求出来,两项相加取最大值就可以了。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <queue>#include <stack>#include <cmath>#include <algorithm>using namespace std;#define maxn 2000 + 10#define INF 10000001int n,x;int grap[maxn][maxn],d[maxn],vis[maxn],ba[maxn];void Dij(){    for(int i = 1; i <= n; i++)    {        d[i] = grap[i][x];        ba[i] = grap[x][i];    }    memset(vis,0,sizeof(vis));    for(int i = 1; i <= n; i++)    {        int u ;        int minn = INF;        for(int j = 1; j <= n; j++)            if(!vis[j] && minn > d[j])            {                u = j;                minn = d[j];            }        vis[u] = 1;        for(int j = 1; j <= n; j++)            if(!vis[j] && (grap[j][u] + minn < d[j]))                d[j] = grap[j][u] + minn;    }    memset(vis,0,sizeof(vis));    for(int i = 1; i <= n; i++)    {        int u ,minn = INF;        for(int j = 1; j <= n; j++)            if(!vis[j] && ba[j] < minn)            {                u = j;                minn = ba[j];            }        vis[u] = 1;        for(int j = 1; j <= n; j++)            if(!vis[j] && ba[j] > grap[u][j] + minn)                ba[j] = grap[u][j] + minn;    }}int main(){    int m,u,v,w;    scanf("%d%d%d",&n,&m,&x);    for(int i = 1; i <= n; i++)        for(int j = i; j <= n; j++)            grap[i][j] = grap[j][i] = (i == j?0:INF);    for(int i = 0; i < m; i++)    {        scanf("%d%d%d",&u,&v,&w);        grap[u][v] = min(grap[u][v],w);    }    Dij();    int ans = 0;    for(int i = 1; i <= n; i++)        ans = max(ans,d[i] + ba[i]);    printf("%d\n",ans);    return 0;}


不能直接用Floyd或Dijkstra搞,会超时的。。。。
参考:http://blog.csdn.net/wangjian8006/article/details/7872048
0 0
原创粉丝点击