POJ 3268 迪杰斯特拉图论 置换找最短路

Posted 姿态H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3268 迪杰斯特拉图论 置换找最短路相关的知识,希望对你有一定的参考价值。

题目:https://vjudge.net/problem/POJ-3268

 

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.
 
 
 
题意:一群牛分别从1~n号农场赶往x号农场参加聚会,农场与农场之间的路时单向的,在n个农场之间有m条路,
给出 a ,b , t表示从a号农场到b号农场需要t时间。 每头牛都会选择最短的路,问来回路上(i→x+x→i)花费时间最长的牛花费的时间是多少?
 
 
思路: 正向建立图查x到各点最短路,,,,在置换mp[j][i]=mp[i][j];;再查x到各点的最短路   然后两次求得最短路相加查找最大值      (     复杂度    O(n*n)) 
 
 
 
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#define maxn 200005
#define inf 100000000
using namespace std;
int dis[maxn];
int mp[1005][1005];
int n,m,x;
int v,u,cost;
bool vis[maxn];
int cnt[maxn];
void dijkstra(int n,int v)
{
    bool vis[maxn];
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dis[i]=mp[v][i];
    }
    dis[v]=0;
    vis[v]=true;
    for(int i=2;i<=n;i++)
    {
            int u=v;
            int mazz=inf;
            for(int j=1;j<=n;j++){
        if(!vis[j]&&dis[j]<mazz)//换dis最小的顶点继续查找
        {
                u=j;
                mazz=dis[j];
        }
        }
        vis[u]=true;
        for(int k=1;k<=n;k++)//更新顶点上的dis
        {
            if(!vis[k]&&mp[u][k]<inf)
            {
                if(dis[k]>mp[u][k]+dis[u]){
                    dis[k]=mp[u][k]+dis[u];
                }
            }
        }

    }
}
int main()
{
   scanf("%d%d%d",&n,&m,&x);
   memset(mp,inf,sizeof(mp));
    //memset(mp,inf,sizeof(mp));
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    mp[i][j]=inf;
    for(int i=0;i<m;i++){
        cin>>u>>v>>cost;
        mp[u][v]=cost;
    }
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
        cnt[i]=inf;
    }
    dijkstra(n,x);
    for(int i=1;i<=n;i++)
    {
        cnt[i]=dis[i];
    }
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
    }
   for(int i=1;i<=n;++i)
        {
            for(int j=i+1;j<=n;++j)
            {
                int aa;
                aa=mp[j][i];
                mp[j][i]=mp[i][j];
                mp[i][j]=aa;
            }
        }
        dijkstra(n,x);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(i!=x);
            ans=max(ans,dis[i]+cnt[i]);
        }
        printf("%d\n",ans);
}

简单的图论思路题  ,,,不过题目挺好的

 

 

以上是关于POJ 3268 迪杰斯特拉图论 置换找最短路的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3268 Silver Cow Party

poj 2253

求多重邻接表的迪杰斯特拉算法

迪杰斯特拉算法为啥不能有负权边

图论基础知识.

迪杰斯特拉算法的算法思想