POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题)

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 39754   Accepted: 14602

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.

先上Bellman-Ford算法。
借鉴自:http://blog.csdn.net/niushuai666/article/details/6791765
算法非常简单。
第一,初始化所有点。每一个点保存一个值,表示从原点到达这个点的距离,将原点的值设为0,其它的点的值设为无穷大(表示不可达)。
第二,进行循环,循环下标为从1到n-1(n等于图中点的个数)。在循环内部,遍历所有的边,进行松弛计算。
第三,遍历途中所有的边(edge(u,v)),判断是否存在这样情况:
d(v) > d (u) + w(u,v)
若存在则返回false,表示途中存在从源点可达的权为负的回路。
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3
const int N=100005;
const int mod=1e9+7;

int f,n,m,w;
int d[505];
struct Edge{
    int u,v,cost;
}edge[N];

bool bellman_ford(){
    for (int i=1; i<n; i++) {
        for (int j=1; j<=2*m+w; j++) {
            if (d[edge[j].v]>d[edge[j].u]+edge[j].cost) {
                d[edge[j].v]=d[edge[j].u]+edge[j].cost;
            }
        }
    }
    bool flag=true;
    for (int j=1; j<=2*m+w; j++)
        if (d[edge[j].v]>d[edge[j].u]+edge[j].cost) {
            flag=false;
            break;
        }
    return flag;
}

int main(){
    int s,e,t;
    cin>>f;
    while (f--) {
        cin>>n>>m>>w;
        memset(d, INF, sizeof(d));
        for (int i=1; i<=2*m; i+=2) {
            scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].cost);
            edge[i+1].u=edge[i].v;
            edge[i+1].v=edge[i].u;
            edge[i+1].cost=edge[i].cost;
        }
        for (int i=1; i<=w; i++) {
            scanf("%d %d %d",&edge[i+2*m].u,&edge[i+2*m].v,&edge[i+2*m].cost);
            edge[i+2*m].cost*=-1;
        }
        if (!bellman_ford()) {
            cout<<"YES\n";
        }
        else {
            cout<<"NO\n";
        }
    }
    return 0;
}

下面是SPFA算法:http://baike.baidu.com/link?url=FNAoppxXYYnhHZI1MGJnZTs9-2MBZPG08RkqHEFWYHDUfrDW2zYd0AjfxthdCcUqM6FrvWEcr90mKTvJ1Gk_jqu9O3aRS5dm85d3-_cI_OzVocyLMGdY7p78E5IXiiheVsMHzu6CFD_dgOJP2xpgMa
找的百度百科资料,照本宣科的用,感觉和bellman-ford算法大意相同。
我们用数组d记录每个结点的最短路径估计值,而且用邻接表来存储图G。我们采取的方法是动态逼近法:设立一个先进先出的队列用来保存待优化的结点,优化时每次取出队首结点u,并且用u点当前的最短路径估计值对离开u点所指向的结点v进行松弛操作,如果v点的最短路径估计值有所调整,且v点不在当前的队列中,就将v点放入队尾。这样不断从队列中取出结点来进行松弛操作,直至队列空为止若有节点放入队列的次数大于n(结点个数),那么就存在负环。
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3
const int N=100005;
const int mod=1e9+7;

int f,n,m,w;
int d[505],cnt[505];
vector<pair<int, int> > a[505];
bool in_queue[505];
queue<int> q;

bool SPFA(int st){
    memset(d, 0x3f, sizeof(d));
    memset(in_queue, 0, sizeof(in_queue));
    memset(cnt, 0, sizeof(cnt));
    d[st]=0;
    q.push(st);
    in_queue[st]=1;
    cnt[st]=1;
    while (!q.empty()) {
        int u=q.front();
        if (cnt[u]>n) {
            return false;
        }
        for (int i=0; i<a[u].size(); i++) {
            pair<int, int> v=a[u][i];
            if (d[u]+v.second<d[v.first]) {
                d[v.first]=d[u]+v.second;
                if (!in_queue[v.first]) {
                    q.push(v.first);
                    in_queue[v.first]=1;
                    cnt[v.first]++;
                }
            }
        }
        q.pop();
        in_queue[u]=0;
    }
    return true;
}

int main(){
    int s,e,t;
    cin>>f;
    while (f--) {
        cin>>n>>m>>w;
        while (!q.empty()) {
            q.pop();
        }
        for (int i=0; i<=n; i++) {
            a[i].clear();
        }
        for (int i=1; i<=m; i++) {
            scanf("%d %d %d",&s,&e,&t);
            a[s].push_back(make_pair(e, t));
            a[e].push_back(make_pair(s, t));
        }
        for (int i=1; i<=w; i++) {
            scanf("%d %d %d",&s,&e,&t);
            a[s].push_back(make_pair(e, -t));
        }
        int flag=1;
        for (int i=1; i<=n; i++) {
            if (!SPFA(i)) {
                flag=0;
                break;
            }
        }
        if (flag) {
            cout<<"NO\n";
        } else {
            cout<<"YES\n";
        }
    }
    return 0;
}

你可能感兴趣的:(POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题))