POJ 3259 Wormholes (判负环)

Wormholes

题目链接:

http://acm.hust.edu.cn/vjudge/contest/122685#problem/F

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.


题意:


平面上的N个点,有M条正常的道路,有W条长度为负的道路.
求是否存在负环.


题解:


以下分别用bellman-ford和spfa实现.


代码:

bellman-ford:

#include
#include
#include
#include
#include
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 6000
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int m,n,k;
int edges, u[maxn], v[maxn], w[maxn];
int first[maxn], next[maxn];
int dis[maxn];

void add_edge(int s, int t, int val) {
    u[edges] = s; v[edges] = t; w[edges] = val;
    next[edges] = first[s];
    first[s] = edges++;
}

bool bellman(int s) {
    for(int i=1; i<=n; i++) dis[i]=inf; dis[s] = 0;

    //for(int i=1; i dis[u[e]]+w[e]){
            dis[v[e]] = dis[u[e]] + w[e];
            if(i == n) return 0;
        }
    }

//    for(int e=0; e dis[u[e]]+w[e])
//            return 0;

    return 1;
}


int main(int argc, char const *argv[])
{
    //IN;

    int t; cin >> t;
    while(t--)
    {
        cin >> n >> m >> k;
        edges = 0;
        for(int i=1; i<=m; i++) {
            int u,v,w; scanf("%d %d %d",&u,&v,&w);
            add_edge(u,v,w);
            add_edge(v,u,w);
        }
        for(int i=1; i<=k; i++) {
            int u,v,w; scanf("%d %d %d",&u,&v,&w);
            add_edge(u,v,-w);
        }

        bool flag = bellman(1);

        if(!flag) puts("YES");
        else puts("NO");
    }

    return 0;
}

spfa:

#include
#include
#include
#include
#include
#include
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 6000
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int m,n,k;
int edges, u[maxn], v[maxn], w[maxn];
int first[maxn], next[maxn];
int dis[maxn];

void add_edge(int s, int t, int val) {
    u[edges] = s; v[edges] = t; w[edges] = val;
    next[edges] = first[s];
    first[s] = edges++;
}

queue q;
bool inq[maxn];
int inq_cnt[maxn];
bool spfa(int s) {
    memset(inq, 0, sizeof(inq));
    memset(inq_cnt, 0, sizeof(inq_cnt));
    for(int i=1; i<=n; i++) dis[i] = inf; dis[s] = 0;
    while(!q.empty()) q.pop();
    q.push(s); inq_cnt[s]++;

    while(!q.empty()) {
        int p = q.front(); q.pop();
        inq[p] = 0;
        for(int e=first[p]; e!=-1; e=next[e]) if(dis[v[e]] > dis[p]+w[e]){
            dis[v[e]] = dis[p] + w[e];
            if(!inq[v[e]]) {
                q.push(v[e]);
                inq[v[e]] = 1;
                inq_cnt[v[e]]++;
                if(inq_cnt[v[e]] >= n) return 0;
            }
        }
    }

    return 1;
}


int main(int argc, char const *argv[])
{
    //IN;

    int t; cin >> t;
    while(t--)
    {
        cin >> n >> m >> k;
        edges = 0;
        memset(first, -1, sizeof(first));
        for(int i=1; i<=m; i++) {
            int u,v,w; scanf("%d %d %d",&u,&v,&w);
            add_edge(u,v,w);
            add_edge(v,u,w);
        }
        for(int i=1; i<=k; i++) {
            int u,v,w; scanf("%d %d %d",&u,&v,&w);
            add_edge(u,v,-w);
        }

        bool flag = spfa(1);

        if(!flag) puts("YES");
        else puts("NO");
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5751451.html

你可能感兴趣的:(POJ 3259 Wormholes (判负环))