POJ -3268 Silver Cow Party(Dijkstra,置换矩阵)

Silver Cow Party

Time Limit: 2000MSMemory Limit: 65536K

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: 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

刚开始想用Floyd,但是O(n^3)会超时,所以用dijkstra
1、先计算去时的dist[];
2、然后把map[][]矩阵置换,map[i][j]与map[j][i]值互换,这样去时通的路就不通了,不通的路就通了;
3、再次计算回时的dist[];
4、取最大的一组值

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define inf 0x3fffffff
const int N=1005;

int dist1[N], dist2[N];//分别表示去和回时的dist[]
int map[N][N];
int n, m, en;

void dijkstra()
{
    int vis[N]= {0};
    int min;
    vis[en]=1;
    for(int i=1; i<n; i++)
    {
        min=inf;
        int k=1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&min>dist1[j])
            {
                min=dist1[j];
                k=j;
            }
        }
        vis[k]=1;
        for(int j=1; j<=n; j++)
            if(!vis[j]&&dist1[j]>dist1[k]+map[k][j])
                dist1[j]=dist1[k]+map[k][j];
    }
}

int main()
{
    int from, to, dis;
    while(scanf("%d%d%d", &n, &m, &en)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            map[i][i]=0;
            for(int j=1; j<i; j++)
                map[i][j]=map[j][i]=inf;
        }
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d", &from, &to, &dis);
            if(dis<map[from][to])
                map[from][to]=dis;
        }
        for(int i=1; i<=n; i++)
            dist1[i]=map[en][i];
        dijkstra();
        
        for(int i=1; i<=n; i++)//将去时的数据存在dist2[]中
            dist2[i]=dist1[i];
            
        for(int i=1; i<=n; i++)//置换矩阵
            for(int j=1; j<i; j++)
            {
                int t=map[i][j];
                map[i][j]=map[j][i];
                map[j][i]=t;
            }
        for(int i=1; i<=n; i++)
            dist1[i]=map[en][i];
        dijkstra();
        int maxx=0;
        for(int i=1; i<=n; i++)//取最大值
        {
            if(i==en)
                continue;
            if(dist1[i]+dist2[i]>maxx)
                maxx=dist1[i]+dist2[i];
        }
        printf("%d\n", maxx);
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值