poj3268Silver Cow Party——最短路变形

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

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total 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 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

题意:有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 10000001

int 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值