poj3259 判断负权回路(spfa和bellman-ford)

Wormholes

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 62665 Accepted: 23388

Description

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input

Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

Hint

For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

题意:FJ有很多个农场,现在在这些农场中出现了很多的虫洞,通过虫洞你可以从起点到终点并且在时间上回到T秒前,你从一个地方到另一个地方需要的时间是 t 秒,问是否存在这样一个虫洞可以让FJ见到之前的自己,。

思路:把从某地到某地花费的时间看做一个正权值,虫洞回溯的时间看成负权值,就变成了一个判断图中是否存在负权回路的最短路问题了,注意,本题每两个点之间必直接或间接相连,所以我们直接从点1出发,看成求点1到所有点的最短距离的最短路问题,用spfa或者bellman-ford判断是否存在负权回路都可以。

AC代码:

bellman-ford:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<cstdlib>
#include<map>
#include<deque>
#include<queue>
#include<list>
const int inf=0x3f3f3f3f;
const int MOD=1e9+7;
#define ll long long
#define ME0(x) memset(x,0,sizeof(x))
#define MEF(x) memset(x,-1,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
using namespace std;
//~~~~~~~~~~~~~~~~~~~~~bellman-ford
struct LX
{
    int st,ed,di;
}lx[6005];
int t,n,a,b,s,e,d,dis[505],cnt;
void add(int x,int y,int z,int c)
{
    lx[c].st=x;
    lx[c].ed=y;
    lx[c].di=z;
}
int bf(int x)
{
    MEI(dis);
    dis[x]=0;
    for(int n1=1;n1<n;n1++)
    {
        for(int cnt1=1;cnt1<=cnt;cnt1++)
        {
            if(dis[lx[cnt1].ed]>dis[lx[cnt1].st]+lx[cnt1].di)
            {
                dis[lx[cnt1].ed]=dis[lx[cnt1].st]+lx[cnt1].di;
            }
        }
    }
    for(int cnt1=1;cnt1<=cnt;cnt1++)
    {
        if(dis[lx[cnt1].ed]>dis[lx[cnt1].st]+lx[cnt1].di)
        {
            return 1;
        }
    }
    return 0;
}
int main()
{
    cin>>t;
    for(int t1=1;t1<=t;t1++)
    {
        cin>>n>>a>>b;
        cnt=0;
        for(int a1=1;a1<=a;a1++)
        {
            cin>>s>>e>>d;
            add(s,e,d,++cnt);
            add(e,s,d,++cnt);
        }
        for(int b1=1;b1<=b;b1++)
        {
            cin>>s>>e>>d;
            add(s,e,-d,++cnt);
        }
        if(bf(1))
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
}

spfa:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<cstdlib>
#include<map>
#include<deque>
#include<queue>
#include<list>
const int inf=0x3f3f3f3f;
const int MOD=1e9+7;
#define ll long long
#define ME0(x) memset(x,0,sizeof(x))
#define MEF(x) memset(x,-1,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
using namespace std;
//~~~~~~~~~~~~~~~~~~~~~~spfa
struct LX
{
    int st,ed,di,next;
}lx[6005];
int first[505];
int t,n,a,b,s,e,d,cnt,vis[505],flag[505],dis[505];
queue<int> q;
void add(int x,int y,int z,int c)
{
    lx[c].st=x;
    lx[c].ed=y;
    lx[c].di=z;
    lx[c].next=first[x];
    first[x]=c;
}
int spfa(int x)
{
    MEI(dis);
    ME0(flag);
    ME0(vis);
    dis[x]=0;
    q.push(x);
    flag[x]=1;
    vis[x]++;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        flag[x]=0;
        for(int i=first[x];i!=0;i=lx[i].next)
        {
            int y=lx[i].ed;
            if(dis[y]>dis[x]+lx[i].di)
            {
                dis[y]=dis[x]+lx[i].di;
                if(!flag[y])
                {
                    q.push(y);
                    flag[y]=1;
                    vis[y]++;
                    if(vis[y]>n)
                    {
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
}
int main()
{
    cin>>t;
    for(int t1=1;t1<=t;t1++)
    {
        cin>>n>>a>>b;
        cnt=0;
        ME0(first);
        for(int a1=1;a1<=a;a1++)
        {
            cin>>s>>e>>d;
            cnt++;
            add(s,e,d,cnt);
            cnt++;
            add(e,s,d,cnt);
        }
        for(int b1=1;b1<=b;b1++)
        {
            cin>>s>>e>>d;
            cnt++;
            add(s,e,-d,cnt);
        }
        if(spfa(1))
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
}

emmmmmmm。。。。。。。水过。。。。。。

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/ecjtu_17_TY/article/details/81480124
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞