我是靠谱客的博主 可靠发带,最近开发中收集的这篇文章主要介绍poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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 ≤XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi requiresTi (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个农场,每个农场有一头牛去参加派对,派对的地点在x农场,一共有m条路。求所有牛到派对和从派对回自己农场最小花费时间之和的最大值。注意的是该图是有向图,从派对农场回自己农场很好求,用一次dijstra,再把邻接矩阵转置再用一i次dijstra,求出各奶牛从自己从自己农场到派对农场的最小花费时间。

#include<stdio.h>

#include<queue>
#include<algorithm>
#include<memory.h>
#include<iostream>
using namespace std;
#define INF 1<<28
#define MAX 1005
int n,m,x,i,j;
int map[MAX][MAX],dto[MAX],dfrom[MAX];
bool visto[MAX],visfrom[MAX];
typedef pair<int ,int>exe;
void dijstrato()
{
    memset(visto,0,sizeof(visto));
    for(i=1;i<=n;i++)
        dto[i]=INF;
    dto[x]=0;
    priority_queue<exe,vector<exe>,greater<exe> >q;
    q.push(make_pair(dto[x],x));
    while(!q.empty())
    {
        exe tmp=q.top();
        q.pop();
        int now=tmp.second;
        if(visto[now])
            continue;
        visto[now]=true;
        for(i=1;i<=n;i++)
            if(!visto[i]&&map[now][i]<INF&&dto[i]>dto[now]+map[now][i])
        {
            dto[i]=dto[now]+map[now][i];
            q.push(make_pair(dto[i],i));
        }
    }
    return;

}
void dijstrafrom()
{
    memset(visfrom,0,sizeof(visfrom));
    for(i=1;i<=n;i++)
      dfrom[i]=INF;
    dfrom[x]=0;
    priority_queue<exe,vector<exe>,greater<exe> >q;
    q.push(make_pair(dfrom[x],x));
    while(!q.empty())
    {
        exe tmp=q.top();
        q.pop();
        int now=tmp.second;
        if(visfrom[now])
            continue;
        visfrom[now]=true;
        for(i=1;i<=n;i++)
            if(!visfrom[i]&&map[now][i]<INF&&dfrom[i]>map[now][i]+dfrom[now])
            {
                dfrom[i]=dfrom[now]+map[now][i];
                q.push(make_pair(dfrom[i],i));
            }
    }
    return ;

}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&x))
    {
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            map[i][j]=INF;
            int x,y,cost;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&cost);
            map[x][y]=cost;
        }
        dijstrato();
        for(i=1;i<=n;i++)
            for(j=i+1;j<=n;j++)
            swap(map[i][j],map[j][i]);
        dijstrafrom();
        priority_queue<int>p;
        for(i=1;i<=n;i++)
            p.push(dto[i]+dfrom[i]);
        printf("%dn",p.top());

    }
    return 0;
}

最后

以上就是可靠发带为你收集整理的poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)的全部内容,希望文章能够帮你解决poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部