POJ 3259 Wormholes (bellman_ford算法判负环)

Wormholes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 32393   Accepted: 11771

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..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..M+W+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 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.

Source

USACO 2006 December Gold

题意:John有个n个农场,所有农场之间有m条路,每条路都有通过所需的时间。有一天,他在农场转悠,竟然发现了w个虫洞(估计这孩子小学物理学的不错~~~),每个虫洞然都有倒退的时间,他就想利用这些虫洞回到以前。问他能不能实现这个神奇之旅。

解析:就是单纯的判断是否有负环,因为如果有负环的话,就代表着回到原点的时间在出发之前,这就会到的以前。不过要特别注意路是双向的,但是虫洞确是单向的!!!级的数组一定要开的足够大,不然就RE,因为路是双向的,所以存路就需要2*MAX_E,再加上MAX_W个虫洞,总共就需要开到 2*MAX_E + MAX_W 才可以。

PS:bellman_ford算法判断负环有两种方法,但是两者的实质都是一样的,不用纠结的,如果直接就只是判断有无负环,还是版本二好写一点。

AC代码:

判负环版本一:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1234567
#define MAX_V 505
#define MAX_E 5205

struct edge{ int s, e, w; };
edge es[MAX_E];
int d[MAX_V];
int E, V;

bool Bellman_ford(int s){
    for(int i=1; i<=V; i++) d[i] = (i==s) ? 0 : INF;
    for(int i=0; i<V-1; i++){
        int flag = 0;
        for(int j=0; j<E; j++){
            edge now = es[j];
            if(d[now.e] > d[now.s] + now.w){
                d[now.e] = d[now.s] + now.w;
                flag = 1;
            }
        }
        if(flag == 0) break;
    }

    for(int i=0; i<E; i++){                 //判负环
        edge now = es[i];
        if(d[now.e] > d[now.s] + now.w)
            return true;
    }
    return false;
}

int main(){
//    freopen("in.txt", "r", stdin);
    int t, n, m, w, s, e, time;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d%d", &n, &m, &w);
        V = n;                             //顶点数
        E = 0;                             //边数
        for(int i=0; i<m; i++){            //路是双向的
            scanf("%d%d%d", &s, &e, &time);
            es[E].s = s;
            es[E].e = e;
            es[E++].w = time;
            es[E].s = e;
            es[E].e = s;
            es[E++].w = time;
        }
        for(int i=0; i<w; i++){          //虫洞是单向!!!
            scanf("%d%d%d", &s, &e, &time);
            es[E].s = s;
            es[E].e = e;
            es[E++].w = -time;
        }
        printf("%s\n", flag ? "YES" : "NO");
    }
    return 0;
}

判负环版本二:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1234567
#define MAX_V 505
#define MAX_E 5205

struct edge{ int s, e, w; };
edge es[MAX_E];
int d[MAX_V];
int E, V;

bool Bellman_ford(int s){
    for(int i=1; i<=V; i++) d[i] = (i==s) ? 0 : INF;
    for(int i=0; i<V; i++){
        for(int j=0; j<E; j++){
            edge now = es[j];
            if(d[now.e] > d[now.s] + now.w){
                d[now.e] = d[now.s] + now.w;
                if(i == V-1) return true;
            }
        }
    }
    return false;
}

int main(){
//    freopen("in.txt", "r", stdin);
    int t, n, m, w, s, e, time;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d%d", &n, &m, &w);
        V = n;
        E = 0;
        for(int i=0; i<m; i++){            //路是双向的
            scanf("%d%d%d", &s, &e, &time);
            es[E].s = s;
            es[E].e = e;
            es[E++].w = time;
            es[E].s = e;
            es[E].e = s;
            es[E++].w = time;
        }
        for(int i=0; i<w; i++){          //虫洞是单向!!!
            scanf("%d%d%d", &s, &e, &time);
            es[E].s = s;
            es[E].e = e;
            es[E++].w = -time;
        }
        printf("%s\n", Bellman_ford(1) ? "YES" : "NO");
    }
    return 0;
}

小编福利:如果想求出来所有的负环的话,只需要把将版本二中的d数组全部初始化为0即可~~~

时间: 2024-07-28 19:13:06

POJ 3259 Wormholes (bellman_ford算法判负环)的相关文章

poj 3259 Wormholes(bellman-ford判断负环)

题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判断有没有负环就行了. #include <iostream> #include <cstring> using namespace std; const int inf = 10001; int f , n , m , w ,dis[1001] , counts; struct TnT

POJ 3259 虫洞旅行 spfa判负环

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

(简单) 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

解题报告: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算法题解

本题其实也可以使用SPFA算法来求解的,不过就一个关键点,就是当某个顶点入列的次数超过所有顶点的总数的时候,就可以判断是有负环出现了. SPFA原来也是可以处理负环的. 不过SPFA这种处理负环的方法自然比一般的Bellman Ford算法要慢点了. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const

UVA - 11090 Going in Cycle!! (Bellman-Ford算法判负环)

Description I I U P C 2 0 06 Problem G: Going in Cycle!! Input: standard input Output: standard output You are given a weighted directed graph with n vertices and m edges. Each cycle in the graph has a weight, which equals to sum of its edges. There

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 wor

UVA558 - Wormholes(BellmanFord判负环)

UVA558 - Wormholes 题目大意: 有一个教授希望利用虫洞回到过去(还是从这个虫洞出来就到达了过去),给你虫洞形成的有向图,问教授能否回到过去. 解题思路: 利用BellmanFord判负环,如果不存在负环的话,那么最多经过N - 1次迭代就可以得到最短路,因为形成最短路最多N - 1个节点(起点不算),但是如果存在了负环,那么就可以一直迭代,最短路会越来越小.可以利用这个性质来判断是否存在负环. 代码: #include <cstdio> #include <cstrin