最短路(SPFA+负权回路的判断)-poj3268

来源:互联网 发布:网络管理吧 编辑:程序博客网 时间:2024/05/17 23:06
Language:
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10887 Accepted: 4831

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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3

Sample Output

10思路:spfa,关键是去得路不好求,可以把边反向,然后求一次最短路即可。红色部分自己以后注意!!!
#include<iostream>#include<set>#include<map>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;const int INF=100000000;struct node{    int u,next,time;} cow1[100010],cow2[100010];int pre1[1010],pre2[1010];LL dis1[1010],dis2[1010];bool vis[1010];int cnt[1010];int N,M,X;void spfa(node *cow,LL *dis,int *pre){    for(int i=1; i<=N; i++)    {        dis[i]=INF;        vis[i]=false;    }    vis[X]=true;    dis[X]=0;    queue<int> q;    memset(cnt,0,sizeof(cnt));    q.push(X);    while(!q.empty())    {        int t=q.front();        q.pop();        vis[t]=0;        for(int j=pre[t]; j!=0; j=cow[j].next)        {            int v=cow[j].u;            if(dis[v]>dis[t]+cow[j].time)            {                dis[v]=dis[t]+cow[j].time;                if(!vis[v])                {                    vis[v]=true;                    q.push(v);                    if(++cnt[v]>N)                        return;                }            }        }    }}int main(){    //freopen("in.txt","r",stdin);    int a,b,t;    while(cin>>N>>M>>X)    {        int e=1;        memset(pre1,0,sizeof(pre1));        memset(pre2,0,sizeof(pre2));        for(int i=0; i<M; i++)        {            cin>>a>>b>>t;            getchar();            cow1[e].next=pre1[a];            cow1[e].u=b;            cow1[e].time=t;            cow2[e].next=pre2[b];            cow2[e].u=a;            cow2[e].time=t;            pre1[a]=pre2[b]=e++;        }        spfa(cow1,dis1,pre1);        spfa(cow2,dis2,pre2);        int min1=0;        for(int i=1; i<=N; i++)        {            int m=dis1[i]+dis2[i];            if(min1<m)                min1=m;        }        cout<<min1<<endl;    }    return 0;}


原创粉丝点击