码迷,mamicode.com
首页 > 编程语言 > 详细

POJ 3259 Wormholes (bellman_ford算法判负环)

时间:2015-01-16 13:07:27      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:

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




题意: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即可~~~




POJ 3259 Wormholes (bellman_ford算法判负环)

标签:

原文地址:http://blog.csdn.net/u013446688/article/details/42774755

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!