poj 3259 Wormholes
来源:互联网 发布:网络剧怎么赚钱 编辑:程序博客网 时间:2024/04/30 06:24
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.
题意分析:大体意思就是有f个农场,有n个点,有m条双向路,有w个单向虫洞连接着个点,构造最短路径,看是否存在负环,就是通过虫洞倒退时间看能够存在负数环,来使一个点的出发时间小于到达时间,用bellman就可以过
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>using namespace std;#define ll long longconst int maxn=5202;struct node{ int u,v,w;};node edge[maxn]; int n,m,p,dist[maxn],k;void addedge(int u,int v,int w){ edge[k].u=u; edge[k].v=v; edge[k].w=w; k++;}bool bellman_ford(){ int i,j; memset(dist,0,sizeof(dist)); for (i=1;i<=n;i++) for (j=0;j<k;j++) if (dist[edge[j].u]+edge[j].w<dist[edge[j].v]) dist[edge[j].v]=dist[edge[j].u]+edge[j].w; for (j=0;j<k;j++) if (dist[edge[j].u]+edge[j].w<dist[edge[j].v]) return 0; return 1;}int main(){ int t; int i,u,v,w; scanf("%d",&t); while (t--) { k=0; scanf("%d%d%d",&n,&m,&p); for (i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } for (i=0;i<p;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,-w); } if (bellman_ford()==0) printf("YES\n"); else printf("NO\n"); } return 0;}
spfa算法一样
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>using namespace std;#define ll long longconst int INF=1<<30;const int maxn=5202;struct node{ int v,w,next;};node edge[maxn];int m,n,p,k,dist[maxn],head[maxn],cnt[maxn];bool vis[maxn];void addedge(int u,int v,int w){ edge[k].v=v; edge[k].w=w; edge[k].next=head[u]; head[u]=k++;}bool spfa(){ int i; memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); for (i=0;i<k;i++) dist[i]=INF; cnt[1]++; dist[1]=0; vis[1]=1; queue<int>q; q.push(1); while (!q.empty()) { int x=q.front(); q.pop(); vis[x]=0; for (int e=head[x];e!=-1;e=edge[e].next) { if (dist[edge[e].v]>dist[x]+edge[e].w) { dist[edge[e].v]=dist[x]+edge[e].w; if (!vis[edge[e].v]) { q.push(edge[e].v); vis[edge[e].v]=1; cnt[edge[e].v]++; if (cnt[edge[e].v]>=n) return 0; } } } } return 1;}int main(){ int t,i,j,u,v,w; scanf("%d",&t); while (t--) { k=0; scanf("%d%d%d",&n,&m,&p); memset(head,-1,sizeof(head)); for (i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } for (i=0;i<p;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,-w); } if (spfa()) printf("NO\n"); else printf("YES\n"); } return 0;}
- poj 3259 Wormholes //SPFA
- POJ 3259 Wormholes
- poj 3259 Wormholes
- POJ 3259 Wormholes
- POJ 3259 Wormholes
- Poj 3259 Wormholes
- poj 3259 Wormholes
- poj-3259-Wormholes
- POJ-3259-Wormholes
- POJ 3259 Wormholes bellman_ford
- poj 3259 Wormholes
- poj 3259 Wormholes
- POJ 3259 Wormholes
- POJ 3259 Wormholes
- POJ 3259 Wormholes
- poj 3259 Wormholes
- POj 3259 Wormholes
- POJ 3259 Wormholes
- 拖欠的Java笔记
- OpenCV 3 - 编译更强大的OpenCV(三) - BUG与崩溃齐飞
- C++实现操作系统进程调度(时间片轮转法)
- [十一]java作业
- 51Nod 1278 相离的圆
- poj 3259 Wormholes
- python file
- Spring MVC 静态概念
- (笔记)Spring MVC学习指南_数据绑定和表单标签库
- python 正则表达式
- Windows内核中的内存管理
- Why are arrays of references illegal?
- 普通平衡树 treap
- echarts饼图文字重叠问题