POJ3259(Wormholes) 判断负环

题意:

农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M

条路,和W个 虫洞组成,FJ想从一块土地开始,经过若干条路和虫洞,返回到他最初开始走的地方并且时间要在他离开之前,或者恰好等于他离开的时间。

把虫洞的时间看成负边权,就是是否存在负权回路。  虽然题中没有说明起点和终点 但从1开始即可  因为无论从哪个点开始 有没有负环的情况都是一样的

spfa 判断负环:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const int maxn = 1001, INF = 0xfffffff;
int vis[maxn], d[maxn], head[maxn], ans[maxn];
int n, m, p;
struct node{
    int u,v,w,next;
}Node[maxn];

void add(int u,int v,int w,int i)
{
    Node[i].u = u;
    Node[i].v = v;
    Node[i].w = w;
    Node[i].next = head[u];
    head[u] = i;
}

int spfa(int s)
{
    queue<int> Q;
    mem(vis,0);
    for(int i=0; i<=n; ++i) d[i] = INF;
    d[s] = 0;
    Q.push(s);
    vis[s] = 1;
    while(!Q.empty())
    {
        int x = Q.front(); Q.pop();
        vis[x] = 0;
        for(int i=head[x]; i!=-1; i=Node[i].next)
        {
            node e = Node[i];
            if(d[e.v] > d[x] + e.w)
            {
                d[e.v] = d[x] + e.w;
                Q.push(e.v);
                vis[e.v] = 1;
                if(++ans[e.v] > n) return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        mem(G,0);
        mem(ans,0);
        mem(head,-1);
        mem(Node,0);
        int ok = 0;
        scanf("%d%d%d",&n,&m,&p);
        int cnt = 0;
        for(int i=0; i<m; ++i)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w,cnt++);
            add(v,u,w,cnt++);
        }
        for(int i=0; i<p; ++i)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,-w,cnt++);
        }

        if(spfa(1)) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/9145841.html

时间: 2024-08-02 03:13:42

POJ3259(Wormholes) 判断负环的相关文章

[kuangbin带你飞]专题四 最短路练习 F - Wormholes (判断负环)

F - Wormholes 题目链接:https://vjudge.net/contest/66569#problem/F 题目: 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

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

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

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,in

spfa判断负环

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

【BZOJ1690】【Usaco2007 Dec】奶牛的旅行 分数规划 判断负环

题解: 分数规划+判断负环. 代码: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 1010 #define M 5050 #define eps 1e-8 using namespace std; double mid,fun[N]; struct Eli { int v,n; do

spfa 判断负环 (转载)

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

POJ #1860 Currency Exchange 最短路径算法 判断负环

Description 题目描述在这里:链接 更多的样例:链接 思路 我们把每种货币看成图的顶点,而每个交换站点实现一对货币的交换,可认为增加一个交换站点就是增加两个顶点间的一个环路.从样例中可以知道,如果想要让NICK的资金增加,那么就需要存在一个权值为正的环路,使得货币总价值能够无限上升. 所以现在的问题变成了如何判断图中是否有正环.由于货币交换后总价值可能降低,也就是说可能存在负权边,那么 Dijkstra 就不适用了,应该采用能判断负环的 Bellman_ford ,还有它的优化算法 S

一种科学的判断负环的方法

判断负环的方法 ------------ 这里有个叫分界线的家伙突然想说,本章主体思路都是在 SPFA 上的o ------------ 感觉分为两种大方向,\(BFS\) 和 \(DFS\) 快速写一下 \(BFS\) 的思路 由 \(SPFA\) 的算法可以发现,如果要更新一个点的 \(dis\) ,那么一定有一个点先被更新了以后,然后通过这个新更新的点来更新这个点,那么在没有负环的情况下,一个点能被更新的最多次数为 \(n\) 次,也就是说,如果一个点进队的次数大于 \(n\) 了,嘿嘿嘿

POJ-3259 Wormholes(判断负环、模板)

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