码迷,mamicode.com
首页 > 其他好文 > 详细

poj3268俩次SPFA。。。。。

时间:2014-08-13 22:35:17      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:poj   spfa   水题   

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

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.

俩次SPFA:

题意:

        N个农场,每个农场的都有一个奶牛去参加party,M条单向路。

求对于所有奶牛来说花费在路上的最长时间(奶牛都是选择最短的路)


1:以X为源点,各顶点的出边构成邻接表,求源点到各顶点的最短路。

2:以X为源点,各顶点的入边做出边,构成邻接表,求源点到各顶电的最短路。

将两次时间加起来最大的即为答案。

PS(构建邻接表推荐刘汝佳小白上方法)

详细请见代码注释:

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x3f // 一个大数
#define eps 1e-6
using namespace std;
#define maxn 1010
#define Maxn 100010
int n,m,x;
int first[maxn];
int next[maxn];
int u[Maxn];
int v[Maxn];
int w[Maxn];
int dis[maxn];
int go[maxn];
int vist[maxn];//记录点在不在队列里面
int a[Maxn],b[Maxn],c[Maxn];
queue<int> Q;
void init()
{
    memset(vist,0,sizeof(vist));
    while(!Q.empty()) Q.pop();//一次spfa后记得清空队列
    memset(dis,INF,sizeof(dis));
    dis[x]=0;
    for(int i=1;i<=n;i++)
    first[i]=-1;
}
void spfa(int x)
{
    Q.push(x);
    vist[x]=1;//入列记为 1
    while(!Q.empty())
    {
        int t=Q.front();      
        Q.pop();
        vist[t]=0;//出列记为 0
        for(int i=first[t];i!=-1;i=next[i])
        {
            if(dis[t]+w[i]<dis[v[i]])
            {
                 dis[v[i]]=dis[t]+w[i];
                 if(!vist[v[i]])
                 {
                        Q.push(v[i]);
                        vist[v[i]]=1;
                 }

            }
        }
    }
    for(int i=1;i<=n;i++)
        go[i]+=dis[i];
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&x))
    {
          init();
          memset(go,0,sizeof(go));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a[i],&b[i],&c[i]);
            u[i]=a[i]; v[i]=b[i]; w[i]=c[i];
            next[i]=first[u[i]];  //推荐刘汝佳小白上邻接表的简单写法
            first[u[i]]=i; //把后入队的放在队首
        }
        spfa(x);//以X为源点,各顶点的出边构成邻接表,求源点到各顶点的最短路。
        init();
        for(int i=1;i<=m;i++)
        {
            v[i]=a[i]; u[i]=b[i]; w[i]=c[i];
            next[i]=first[u[i]];
            first[u[i]]=i;
        }
        spfa(x); //以X为源点,各顶点的入边构成邻接表,求源点到各顶点的最短路。                        
      int mm=go[1];
     for(int i=2;i<=n;i++)
       if(go[i]>mm)
        mm=go[i];
     printf("%d\n",mm);
    }
    return 0;
}




poj3268俩次SPFA。。。。。,布布扣,bubuko.com

poj3268俩次SPFA。。。。。

标签:poj   spfa   水题   

原文地址:http://blog.csdn.net/u013712847/article/details/38541699

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!