POJ 3259 Wormholes(用Bellman Ford算法解决实际问题)

Wormholes

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 64177 Accepted: 23954

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

思路:

《POJ 3259 Wormholes(用Bellman Ford算法解决实际问题)》

大题就是这个意思,那么我们知道Bellman_ford算法可以处理负权边

所以这里套上Bellman_ford算法的模板就可以。

直接上源代码:

#include<iostream>
#include<vector>
using namespace std;
const int INF=1<<30;
int F,N,M,W;
int dist[1000];
struct Edge{
    int s,e,w;
    Edge(int ss,int ee,int ww):s(ss),e(ee),w(ww){}//构造函数
    Edge(){}
};
vector<Edge> edges;//将edges设置成一维数组
bool Bellman_ford(int v){
    for(int i=1;i<=N;i++)
        dist[i]=INF;
    dist[v]=0;
    //以下几行是Bellman_ford算法的核心代码
    for(int k=1;k<N;k++)
    for(int i=0;i<edges.size();i++){
        int s=edges[i].s;
        int e=edges[i].e;
        if(dist[s]+edges[i].w<dist[e])
            dist[e]=dist[s]+edges[i].w;

    }
    //就是松弛的过程
    for(int i=0;i<edges.size();i++){
        int s=edges[i].s;
        int e=edges[i].e;
        if(dist[s]+edges[i].w<dist[e])
            return true;
    }
    return false;

}
int main(){
    int F;//农场的数量
    cin>>F;
    while(F--){
        edges.clear();//相当于初始化
        cin>>N>>M>>W;//N个点,M条双向正权边,W条单向负权边
        for(int i=0;i<M;i++){
            int s,e,t;
            cin>>s>>e>>t;//按照题目要求输入
            //双向边等于两条边
            edges.push_back(Edge(s,e,t));
            edges.push_back(Edge(e,s,t));
        }
        for(int i=0;i<W;i++){
            int s,e,t;
            cin>>s>>e>>t;
            edges.push_back(Edge(s,e,-t));//输入负权边
        }
        进入函数进行判断
        if(Bellman_ford(1))
            cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }

}

这里用到的原理是:负权环路的判断

证明:
如果成立,则说明找到了一条经过了n n 条边的从 s  到k k 的路径,且其比任何少于n n 条边的从s s 到k k 的路径都短。
一共n 个顶点,路径却经过了n 条边,则必有一个顶点m 经过了至少两次。则m是一个回路的起点和终点。走这个回路比不走这个回路路径更短,只能说明这个回路是负权回路。

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/qq_37618760/article/details/82151025
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞