Wormholes (Bellman - Ford 检验负权回路)

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..NM (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, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2.. M+1 of each farm: Three space-separated numbers ( SET) 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.. MW+1 of each farm: Three space-separated numbers ( SET) that describe, respectively: A one way path from S to E that also moves the traveler backT 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.

题意:在一个农场内存在一些路,同时也存在很多虫洞,虫洞的作用是使时间后退指定秒数且到达指定地点,问能不能回到以前。

思路:如果可以回到过去的话,就说明图中存在负权回路。当任意一点走过n次仍能继续松弛边的时候就说明拥有负权回路。Bellman – Ford可以检验负权回路。

代码如下:

#include<cstdio>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f
#define N 10010
using namespace std;
int n;
int dis[N],u[N],v[N],w[N],first[N],next[N],ans[N],book[N];
void init() //初始化
{
    memset(book,0,sizeof(book));
    memset(ans,0,sizeof(ans));
    memset(dis,inf,sizeof(dis));
    for(int i=1; i<=n; i++)
        first[i]=-1;
}
void bfs()   //Bellman - Ford检验负权回路
{
    queue<int>Q;
    Q.push(1);
    book[1]=1;
    ans[1]++;
    dis[1]=0;
    while(!Q.empty())
    {
        int t=Q.front();//取一个顶点
        Q.pop();
        ans[t]++;  //出现次数+1
        for(int i=first[t]; i!=-1; i=next[i])//i为这个顶点指向哪一个点(v[i])的索引
        {
            if(dis[v[i]]>dis[t]+w[i])    //通过t点跳到这个点的路径比直接到这个点的路径短
            {
                dis[v[i]]=dis[t]+w[i];  //松弛边长
                if(!book[v[i]])        //v[i]不在队列内,入队列,以后还可能通过此点松弛
                {
                    if(ans[v[t]]>=n)  //判断出存在负权回路
                    {
                        printf("YES\n");
                        return;
                    }
                    Q.push(v[i]);    //v[i]入队列
                    ans[v[i]]++;     //这条边松弛次数+1
                    book[v[i]]=1;    //标记
                }
            }
        }
        book[t]=0; //顶点已经出队列了
    }
    printf("NO\n");
    return;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int m,wa;
        scanf("%d%d%d",&n,&m,&wa);
        init();
        for(int i=1,j=1; i<=m; i++,j+=2)
        {
            scanf("%d%d%d",&u[j],&v[j],&w[j]);//回路
            u[j+1]=v[j];
            v[j+1]=u[j];
            w[j+1]=w[j];
        }
        for(int i=2*m+1; i<=2*m+wa; i++)
        {
            scanf("%d%d%d",&u[i],&v[i],&w[i]);  //虫洞
            w[i]=-w[i];
        }
        for(int i=1; i<=2*m+wa; i++)  //建邻接表
        {
            next[i]=first[u[i]];
            first[u[i]]=i;
        }
        bfs();
    }
}

 

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