Wormholes POJ - 3259 spfa判断负环

//判断负环     dist初始化为正无穷
//正环  负无穷
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N=1e5,INF=0x3f3f3f3f;
int dist[N];
int h[N],e[N],ne[N],w[N],idx;
int n,m,z;
void add(int a,int b,int c)
{
    e[idx]=b;
    w[idx]=c;
    ne[idx]=h[a];
    h[a]=idx++;
}
bool st[N];
int times[N];
bool spfa()
{
    memset(st,0,sizeof st);
    memset(times,0,sizeof times);
    queue<int>q;
    st[1]=1;
    dist[1]=0;
    q.push(1);
    times[1]++;
    while(q.size())
    {
        int t=q.front();
        q.pop();
        st[t]=0;
        for(int i=h[t];~i;i=ne[i])
        {
            int j=e[i];
            if(dist[j]>dist[t]+w[i])
            {
                dist[j]=dist[t]+w[i];
                if(!st[j])
                {
                    st[j]=1;
                    q.push(j);
                    times[j]++;
                    if(times[j]>n)
                        return true;
                }
            }
        }
    }
    return false;
}
int main()
{
    int t,a,b,c;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>z;
        idx=0;
        memset(h,-1,sizeof h);
        memset(dist,0x3f,sizeof dist);
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            add(a,b,c);
            add(b,a,c);
        }
        for(int i=1;i<=z;i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            add(a,b,-c);
        }
        if(spfa())
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12235753.html

时间: 2024-12-13 13:06:39

Wormholes POJ - 3259 spfa判断负环的相关文章

POJ 3259 Wormholes【最短路/SPFA判断负环模板】

农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞.FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间.也许他就能遇到自己了:).为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农场的完整的映射到(1≤F≤5).所有的路径所花时间都

spfa 判断负环 (转载)

判断给定的有向图中是否存在负环. 利用 spfa 算法判断负环有两种方法: 1) spfa 的 dfs 形式,判断条件是存在一点在一条路径上出现多次. 2) spfa 的 bfs 形式,判断条件是存在一点入队次数大于总顶点数. 代码如下: 法 1 (spfa 的 dfs 形式): #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int oo = 1 &

POJ3259 Wormholes(SPFA判断负环)

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! Eac

spfa判断负环

会了spfa这么长时间竟然不会判断负环,今天刚回.. [例题]poj3259 题目大意:当农场主 John 在开垦他的农场时,他发现了许多奇怪的昆虫洞.这些昆虫洞是单向的,并且可以把你从入口送到出口,并且使得时间倒退一段时间. John 的每个农场包含 N(1≤N≤500)块地,编号从 1-N,这 N 块地之间有 M(1≤M≤2500)条路. W(1≤W≤200)个昆虫洞.因为 John 是一个狂热的时间旅行迷,他想尝试着做这样一件事:从某块地出发,通过一些路径和昆虫洞,返回到出发点,并且时间早

Currency Exchange POJ - 1860 spfa判断正环

//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace std; const int N=1e4; const int INF=2e9; int h[N],to[N],ne[N],idx; double r[N],c[N]; int n, m,X; double V; void add(int u,int v,double r1,double c1) { to[id

(简单) POJ 3259 Wormholes,SPFA判断负环。

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! Eac

poj 3259 Wormholes【spfa判断负环】

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36729   Accepted: 13444 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 p

POJ3259 Wormholes 【SPFA判断负环】

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32111   Accepted: 11662 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 p

uva 558 Wormholes (Bellman-Ford算法判断负环)

uva 558 Wormholes In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties: Wormholes are one-way only. The time it takes to travel throu