POJ 3259 Wormholes bellman-ford判负环

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个负边,后面M行M行正边,双向,W条负边,单向。有负环,输出YES,没有输出NO。跑一个bellman_ford就行了。
bellman_ford原理:n个点,如果由起点到每一个点有最短路,那么由邻近的边开始类似dij的更新(他们都叫松弛,我不懂),那么剩余n-1个点,最多n-1次更新就可以找出到每个点的最短路。如果存在负环,那么这个负环可以一直跑,最短路为-inf,这样就不存在到任何点的最短路。因为已经跑了n-1次了,能最短路的都最短怒了,所以,只需要循环找一遍是否存在可以继续更新的边,如果存在,则代表存在有负环。

中间使用了一个flag,如果有点能更新就一直继续,如果不能则直接退出循环,能提高运算效率。使用结构体的储存边之间关系在这道题中效率比邻接矩阵效率高,邻接表还不会,不会讨论复杂度。而且感觉也挺好写易懂。

/// poj3259
/// 2015/11/4

#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"string.h"
#define inf 77777777;
using namespace std;

struct node
{
    int u,v,w;    ///u -> v 有一条边 u到v边的权值为w
}edge[6010];
int n,m,w;   ///n个点,m条正边,w条负边
int dis[510];   ///用来更新是否存在最短路

bool bellman_ford()
{
    bool flag;
    for(int i = 1;i <= 505;i++)   ///初始化
        dis[i] = inf;
    dis[1] = 0;
    for(int i = 1;i <= n-1 && !flag;i++)   ///n-1次更新
    {
        flag = true;
        for(int j = 1;j <= m;j++)
        {
            if(dis[edge[j].v] > dis[edge[j].u]+edge[j].w)   ///是否能更新
            {
                dis[edge[j].v] = dis[edge[j].u]+edge[j].w;
                flag = false;      ///能更新赋值为false
            }
        }
    }
    for(int i = 1;i <= m;i++)     ///找是否存在能继续更新的点,有就存在负环 返回true
        if(dis[edge[i].v] > dis[edge[i].u]+edge[i].w)
            return true;
    return false;
}
int main(void)
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&w);
        for(int i = 1;i <= m;i++)   ///m条正边
        {
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
            edge[i+m].u = edge[i].v;    ///双向边
            edge[i+m].v = edge[i].u;
            edge[i+m].w = edge[i].w;
        }
        for(int i = m*2+1;i <= m*2+w;i++)  ///w条负边
        {
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
            edge[i].w = -edge[i].w;
        }
        m = m*2+w;     ///把m更新为总的边一共有m*2+w条
        if(bellman_ford())    ///有负边输出YES,没有输出NO
            printf("YES\n");
        else
            printf("NO\n");
    }
}

你可能感兴趣的:(poj)