poj 3259 Wormholes 判断负权值回路

Wormholes

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

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.

//Memory Time
//308K   204MS

#include<iostream>
#include <string.h>
using namespace std;

int dis[1001];      //源点到各点权值
const int max_w=10001;      //无穷远

struct weight
{
    int s;
    int e;
    int t;
}edge[5200];

int N,M,W_h; //N (1≤N≤500)fields 顶点数
             //M (1≤M≤2500)paths 正权双向边
             //W_h (1≤W≤200) wormholes 虫洞(回溯),负权单向边
int all_e;   //边集(边总数)

bool bellman()
{
    bool flag;

    /*relax*/

    for(int i=0;i<N-1;i++)  ///dis松弛的次数
    {
        flag=false;
        for(int j=0;j<all_e;j++) ///所有边集
            if(dis[edge[j].e] > dis[edge[j].s] + edge[j].t)  ///可以松弛  更新
            {
                dis[edge[j].e] = dis[edge[j].s] + edge[j].t;
                flag=true;         //relax对路径有更新
            }
        if(!flag)
            break;  //只要某一次relax没有更新,说明最短路径已经查找完毕,或者部分点不可达,可以跳出relax
    }///已经更新完毕了 所有边集都是两点之间的最短路

    /*Search Negative Circle*/

    for(int k=0;k<all_e;k++) ///遍历所有边集 如果还出现能够再次更新成最短的边  就表明出现负权值回路了
        if( dis[edge[k].e] > dis[edge[k].s] + edge[k].t)
            return true;
    return false;
}
int main(void)
{
    int u,v,w;

    int F;
    cin>>F;
    while(F--)
    {
        memset(dis,max_w,sizeof(dis));    //源点到各点的初始值为无穷,即默认不连通

        cin>>N>>M>>W_h;

        all_e=0;      //初始化指针

        /*read in Positive Paths*/

        for(int i=1;i<=M;i++)
        {
            cin>>u>>v>>w;
            edge[all_e].s=edge[all_e+1].e=u;
            edge[all_e].e=edge[all_e+1].s=v;
            edge[all_e++].t=w;
            edge[all_e++].t=w;               //由于paths的双向性,两个方向权值相等,注意指针的移动
        }

        /*read in Negative Wormholds*/

        for(int j=1;j<=W_h;j++)
        {
            cin>>u>>v>>w;
            edge[all_e].s=u;
            edge[all_e].e=v;
            edge[all_e++].t=-w;     //注意权值为负
        }

        /*Bellman-Ford Algorithm*/

        if(bellman())
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
//Memory Time
//308K   204MS

#include<iostream>
#include <string.h>
using namespace std;

int dis[1001];      //源点到各点权值
const int max_w=10001;      //无穷远

struct weight
{
    int s;
    int e;
    int t;
}edge[5200];

int N,M,W_h; //N (1≤N≤500)fields 顶点数
             //M (1≤M≤2500)paths 正权双向边
             //W_h (1≤W≤200) wormholes 虫洞(回溯),负权单向边
int all_e;   //边集(边总数)

bool bellman()
{
    bool flag;

    /*relax*/

    for(int i=0;i<N-1;i++)  ///dis松弛的次数
    {
        flag=false;
        for(int j=0;j<all_e;j++) ///所有边集
            if(dis[edge[j].e] > dis[edge[j].s] + edge[j].t)  ///可以松弛  更新
            {
                dis[edge[j].e] = dis[edge[j].s] + edge[j].t;
                flag=true;         //relax对路径有更新
            }
        if(!flag)
            break;  //只要某一次relax没有更新,说明最短路径已经查找完毕,或者部分点不可达,可以跳出relax
    }///已经更新完毕了 所有边集都是两点之间的最短路

    /*Search Negative Circle*/

    for(int k=0;k<all_e;k++) ///遍历所有边集 如果还出现能够再次更新成最短的边  就表明出现负权值回路了
        if( dis[edge[k].e] > dis[edge[k].s] + edge[k].t)
            return true;
    return false;
}
int main(void)
{
    int u,v,w;

    int F;
    cin>>F;
    while(F--)
    {
        memset(dis,max_w,sizeof(dis));    //源点到各点的初始值为无穷,即默认不连通

        cin>>N>>M>>W_h;

        all_e=0;      //初始化指针

        /*read in Positive Paths*/

        for(int i=1;i<=M;i++)
        {
            cin>>u>>v>>w;
            edge[all_e].s=edge[all_e+1].e=u;
            edge[all_e].e=edge[all_e+1].s=v;
            edge[all_e++].t=w;
            edge[all_e++].t=w;               //由于paths的双向性,两个方向权值相等,注意指针的移动
        }

        /*read in Negative Wormholds*/

        for(int j=1;j<=W_h;j++)
        {
            cin>>u>>v>>w;
            edge[all_e].s=u;
            edge[all_e].e=v;
            edge[all_e++].t=-w;     //注意权值为负
        }

        /*Bellman-Ford Algorithm*/

        if(bellman())
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
时间: 2024-08-03 09:10:51

poj 3259 Wormholes 判断负权值回路的相关文章

poj 3259 Wormholes (负权最短路,SPAF)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36641   Accepted: 13405 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判负权值回路】

题意:有n个点,之间有m条双向路径,还有w个虫洞,单向,从一点到另一点需要花费时间,但是有虫洞的话会减少时间,一个人想要走某一条路使得他能碰到过去的自己,问这个图是否能让他实现他的想法. 其实就是判一个图是否存在负权值回路,SPFA可以实现,原理是:如果存在负权值回路,那么从源点到某个顶点的距离就可以无限缩短,因此就会无限入队,所以在SPFA中统计每个顶点的入队次数,如果超过了n个(顶点个数)则说明存在负权值回路. 我把输出yes和输出no写反了,WA了两发,看了半天都没发现... #inclu

有向图单源非负权值回路最短路径——BellmanFord算法

BellmanFord算法是一种暴力求解算法O(N3),它考虑所有情况,所以可以允许边的权值为负.(不过不允许出现负权值回路,因为那样会出现无限小) 之所以说它暴力,是因为它求出了每个节点所有长度为1的路径,再求所有长度为2的路径,并更新最短路径数组dist[]和path[],如此迭代直至求到长度n-1的路径.(n为图节点个数) 整体来看,每个节点纵向比较,从1到n-1长度的路径中取最小值作为最终路径长度. 因为它考虑了所有情况,所以负边也可以算出. 因为暴力,复杂度比Dijkstra高一个数量

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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 pa

Wormholes POJ - 3259 spfa判断负环

//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int N=1e5,INF=0x3f3f3f3f; int dist[N]; int h[N],e[N],ne[N],w[N],idx; int n,m,z; void add(int a,int b,in

POJ #3259 Wormholes 判负环

Description 问题描述:链接 思路 裸题,判断图是否有负环,用 bellman_ford 或者 spfa . #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<cstring> using namespace std; #define INF 0x3f3f3f3f int N, M, W; //顶点数 正环数 负权边数 con

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 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 &amp; BellmanFord

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)

链接:http://poj.org/problem?id=3259 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 BE