Wormholes (poj 3259 SPFA || Bellman_Ford 判负环)
来源:互联网 发布:大数据txt 编辑:程序博客网 时间:2024/05/22 15:05
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.
Source
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。
很久之前做的最短路,今天又写了一遍,复习了一下SPFA和Bellma_Ford,判断负权值回路。
代码:
//Bellman_Ford#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 550#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;struct Edge{ int u,v,w;}edge[maxn*10];int n,m,w,num;int dist[maxn];bool Bellman_Ford(){ int i,j; FRL(i,0,n+2) dist[i]=INF; dist[1]=0; FRL(i,1,n) { bool flag=false; FRL(j,0,num) { int u=edge[j].u; int v=edge[j].v; if (dist[v]>dist[u]+edge[j].w) { flag=true; dist[v]=dist[u]+edge[j].w; } } if (!flag) return true; } FRL(i,0,num) if (dist[ edge[i].v ]>dist[ edge[i].u ]+edge[i].w) return false; return true;}int main(){ int i,j,cas; sf(cas); while (cas--) { num=0; int u,v,t; sfff(n,m,w); FRL(i,0,m) { sfff(u,v,t); edge[num].u=u; edge[num].v=v; edge[num].w=t; num++; edge[num].u=v; edge[num].v=u; edge[num].w=t; num++; } FRL(i,0,w) { sfff(u,v,t); edge[num].u=u; edge[num].v=v; edge[num].w=-t; num++; } if (Bellman_Ford()) pf("NO\n"); else pf("YES\n"); } return 0;}
//SPFA#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 550#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;struct Edge{ int v,w; int next;}edge[maxn*10];int n,m,w,num;int head[maxn];int dist[maxn];bool inq[maxn];int cnt[maxn];void init(){ num=0; mem(head,-1); mem(inq,false); mem(cnt,0); mem(dist,INF);}void addedge(int u,int v,int w){ edge[num].v=v; edge[num].w=w; edge[num].next=head[u]; head[u]=num++;}bool SPFA(){ queue<int>Q; inq[1]=true; cnt[1]=1; dist[1]=0; while (!Q.empty()) Q.pop(); Q.push(1); while (!Q.empty()) { int u=Q.front(); Q.pop(); inq[u]=false; if (cnt[u]>n) return false; for (int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if (dist[v]>dist[u]+edge[i].w) { dist[v]=dist[u]+edge[i].w; if (!inq[v]) { inq[v]=true; cnt[v]++; Q.push(v); } } } } return true;}int main(){ int i,j,cas; sf(cas); while (cas--) { int u,v,t; init(); sfff(n,m,w); FRL(i,0,m) { sfff(u,v,t); addedge(u,v,t); addedge(v,u,t); } FRL(i,0,w) { sfff(u,v,t); addedge(u,v,-t); } if (SPFA()) pf("NO\n"); else pf("YES\n"); } return 0;}
- Wormholes (poj 3259 SPFA || Bellman_Ford 判负环)
- poj 3259 Wormholes[ bellman_ford 判负环]
- Poj 3259 Wormholes(bellman_ford判负环)
- POJ 3259 Wormholes bellman_ford
- poj 3259 Wormholes (bellman_ford)
- 【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)
- POJ 3259 Wormholes(Bellman_Ford判负圈)
- POJ-3259 Wormholes (bellman_ford算法)
- 【POJ 3259】Wormholes 【spfa判负环】
- poj 3259--Wormholes 【spfa判负环】
- POJ 3259-Wormholes-SPFA判负环
- POJ 3259 Wormholes SPFA判负环
- poj 3259 Wormholes【spfa判负环】
- 25、显示硬件信息(cpu信息、内存信息、硬盘信息、显示屏信息)
- 会做事比做事重要
- AngularJS百度百科
- Java的单例模式和枚举类
- 第2周项目5员工工资之从文件中读取数据
- Wormholes (poj 3259 SPFA || Bellman_Ford 判负环)
- 黑马程序员——Java数组
- tomcat 安装时出现 Failed to install Tomcat7 service
- 插入排序 快速排序 分析整理
- 第三周 【项目5 - 数组作数据成员】(3)
- 天声人語 20150322
- C++宏定义详解
- 第二周【项目3—时间类】
- Latex 公式换行问题,(换行,等号对齐)