POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题)
来源:互联网 发布:php 时间转换为时间戳 编辑:程序博客网 时间:2024/05/16 19:47
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 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
Sample Input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
Hint
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.
第二,进行循环,循环下标为从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 0x3f3f3f3const 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
#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 0x3f3f3f3const 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) (模板题)
- poj 3259 Wormholes(SPFA || Bellman-Ford)
- POJ 3259 Wormholes (SPFA&&BellMan Ford)
- poj 3259 Wormholes【Bellman-Ford Vs SPFA】
- poj 3259 Wormholes SPFA // Bellman-ford
- POJ 3259 Wormholes (Bellman-ford或SPFA)
- Bellman-Ford||SPFA-POJ-3259-Wormholes
- 【POJ】3259 Wormholes bellman-ford | SPFA
- Wormholes( POJ 3259)(Bellman-Ford+SPFA)(判断是否有负权环)(最短路模板)
- POJ - 3259 Wormholes(判断负环, Bellman Ford,SPFA)
- POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)
- POJ 3259-Wormholes(Bellman Ford或SPFA判负权值回路)
- POJ 3259 Wormholes(判断负权回路|SPFA||Bellman-Ford)
- POJ 3259 Wormholes(判断负环&(Bellman-Ford|SPFA))
- POJ 3259 Wormholes(Bellman-Ford)
- poj 3259 Wormholes (Bellman-ford)
- POJ 3259 Wormholes(Bellman-Ford判负环)
- poj 3259 Wormholes(Bellman-Ford)
- js Date日期格式化方法
- php中数组的拆分,合并,分解,连接操作
- Javascript学习--ES6学习--Number.isInteger()方法
- C#操作Word完全方法
- Cube process 或者 deploy 时发生数据库因权限问题无法连接的解决办法
- POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题)
- 事务
- 文章标题
- easyui主界面分析-3
- 20160308 李白喝酒
- android背景选择器selector用法汇总
- iOS测试证书创建,真机测试
- C#-界面假死
- 135.Which three statements are true regarding subqueries? (Choose three.)