poj 3259 Wormholes[ bellman_ford 判负环]

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

思路:普通路双向,正值;虫洞单向,负值;bellman_ford 判负环;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=1002;

int dist[N];
int gra[N][N],vis[N][N];

bool bellman_ford(int n,int s)
{
    for(int i=1;i<=n;i++)
    {
        dist[i]=INF;
    }
    dist[s]=0;
    for(int i=1;i<=n-1;i++)
    {
        int flag=false;
        for(int j=1;j<=n;j++){
            for(int k=1;k<=n;k++){
                if(vis[j][k]==1&&dist[k]>dist[j]+gra[j][k])
                {
                    dist[k]=dist[j]+gra[j][k];
                    flag=true;
                }
            }
        }
        if(!flag) return false;
    }

    for(int j=1;j<=n;j++){
        for(int k=1;k<=n;k++){
            if(vis[j][k]==1&&dist[k]>dist[j]+gra[j][k])
                return true;
        }
    }
    return false;
}

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        int n,m,w;
        scanf("%d%d%d",&n,&m,&w);
        mem(vis,0);
        int s,e,t;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&s,&e,&t);
            if(vis[s][e])
            {
                if(gra[s][e]>t)
                    gra[s][e]=gra[e][s]=t;
            }
            else{
                gra[s][e]=gra[e][s]=t;
                vis[s][e]=vis[e][s]=1;
            }
        }
        for(int i=0;i<w;i++)
        {
            scanf("%d%d%d",&s,&e,&t);
            gra[s][e]=0-t;
            vis[s][e]=1;

        }

        int flag=0;
        for(int i=1;i<=1;i++)       //只用判1,就行了,图是联通的,不然临接矩阵超时!!
        {
            if(bellman_ford(n,i))
            {
                flag=1;
                break;
            }
        }
//        for(int i=1;i<=n;i++)
//            printf("%d___",dist[i]);
//        printf("%d\n",gra[3][1]);
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
时间: 2024-10-11 13:23:20

poj 3259 Wormholes[ bellman_ford 判负环]的相关文章

POJ 3259 Wormholes(SPFA判负环)

题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using na

POJ 3259 Wormholes (判负环)

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

POJ 3259 Wormholes( bellmanFord判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 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判负环

又写了个bellman模板一直RE求解啊... #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x

POJ 3259 Wormholes 虫洞(负权最短路,负环)

题意:给一个混合图,求判断是否有负环的存在,若有,输出YES,否则NO.有重边. 思路:这是spfa的功能范围.一个点入队列超过n次就是有负环了.因为是混合图,所以当你跑一次spfa时发现没有负环,但是负环仍可能存在,因为有向边! 但是单源最短路也有起点啊,难道穷举起点?不用,负环是必须有某些边是带负权的,那么我们只要穷举负权边的起点就行了,因为单单跑一次spfa不能保证能遍历所有点,但是如果穷举负权边起点还没有找到负环,那么负环不可能存在(剩下的都是正权,怎么可能有负环). 1 //#incl

POJ - 3259 Wormholes(求负权回路)

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 fa

「POJ3259」Wormholes - SPFA判负环

->戳我进原题 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 65211 Accepted: 24284 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-w

poj 3259 wormholes AC代码(负权环判断, Bellmanford)

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<iomanip> #include<cstdlib> #include<cstdio> #include<vector> #include<algorithm> #include<cmath> #include<map> using namespa

poj 3259 Wormholes spfa : 双端队列优化 判负环 O(k*E)

1 /** 2 problem: http://poj.org/problem?id=3259 3 spfa判负环: 4 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) 5 spfa双端队列优化: 6 维护队列使其dist小的点优先处理 7 **/ 8 #include<stdio.h> 9 #include<deque> 10 #include<algorithm> 11 using namespace std; 12 13 class Graph