Bellman-Ford||SPFA-POJ-3259-Wormholes
来源:互联网 发布:大型无人机价格 知乎 编辑:程序博客网 时间:2024/06/05 14:58
Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 37628 Accepted: 13850
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: A single integer, F. F farm descriptions follow.
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
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
题目真的很难读懂,大致意思是路径是双向的,而虫洞是单向的,要求是否存在负环,用Bellman-Ford算法就可以了。
//// main.cpp// 最短路练习-F-Wormholes//// Created by 袁子涵 on 15/10/11.// Copyright (c) 2015年 袁子涵. All rights reserved.//// 532ms 800KB#include <iostream>#include <string.h>#include <stdio.h>#include <stdlib.h>#define INF 999999999using namespace std;typedef struct edge{ int S,E; long long int T;}Edge;int F,N,M,W;Edge eg[10000];long long int sum=0,dis[1000];bool bellman_ford(){ bool flag=0; for (int i=1; i<N; i++) { dis[i]=INF; } dis[0]=0; for (int i=0; i<N; i++) { flag=0; for (int j=0; j<sum; j++) { if (dis[eg[j].E]>dis[eg[j].S]+eg[j].T && dis[eg[j].S]<INF) { dis[eg[j].E]=dis[eg[j].S]+eg[j].T; flag=1; } } if (flag==0) { break; } } for (int i=0; i<sum; i++) { if (dis[eg[i].E]>dis[eg[i].S]+eg[i].T && dis[eg[i].S]<INF) { dis[eg[i].E]=dis[eg[i].S]+eg[i].T; return 1; } } return 0;}int main(int argc, const char * argv[]) { cin >> F; int s,e,t; while (F--) { cin >> N >> M >> W; sum=0; for (int i=0; i<M; i++) { cin >> s >> e >> t; eg[sum].S=s-1; eg[sum].E=e-1; eg[sum++].T=t; eg[sum].S=e-1; eg[sum].E=s-1; eg[sum++].T=t; } for (int i=0; i<W; i++) { cin >> s >> e >> t; eg[sum].S=s-1; eg[sum].E=e-1; eg[sum++].T=-t; } if (bellman_ford()) { cout << "YES" << endl; } else cout << "NO" << endl; } return 0;}
只是判断负环而已,所以还能用SPFA做。
//// main.cpp// POJ-3259-Wormholes//// Created by 袁子涵 on 15/11/28.// Copyright © 2015年 袁子涵. All rights reserved.//// 125ms 812KB#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#include <stdlib.h>#include <vector>#include <queue>#define MAXN 505#define INF 0x3f3f3f3fusing namespace std;int f,n,m,w;struct Edge{ int v,cost; Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}};vector<Edge>E[MAXN];void addedge(int u,int v,int w){ E[u].push_back(Edge(v,w));}bool vis[MAXN];int cnt[MAXN];int dist[MAXN];bool SPFA(int start){ memset(vis, 0, sizeof(vis)); for (int i=1; i<=n; i++) { dist[i]=INF; } dist[start]=0; vis[start]=1; queue<int>que; while (!que.empty()) { que.pop(); } que.push(start); memset(cnt, 0, sizeof(cnt)); cnt[start]=1; while (!que.empty()) { int u=que.front(); que.pop(); vis[u]=false; for (int i=0; i<E[u].size(); i++) { int v=E[u][i].v; if (dist[v]>dist[u]+E[u][i].cost) { dist[v]=dist[u]+E[u][i].cost; if (!vis[v]) { vis[v]=1; que.push(v); if (++cnt[v]>n) { return 0; } } } } } return 1;}int main(int argc, const char * argv[]) { cin >> f; int a,b,c; while (f--) { cin >> n >> m >> w; for (int i=1; i<=n; i++) { E[i].clear(); } for (int i=1; i<=m; i++) { scanf("%d%d%d",&a,&b,&c); addedge(a, b, c); addedge(b, a, c); } for (int i=1; i<=w; i++) { scanf("%d%d%d",&a,&b,&c); addedge(a, b, -c); } if(SPFA(1)) printf("NO\n"); else printf("YES\n"); } return 0;}
- 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 (SPFA&&BellMan Ford)
- 【POJ】3259 Wormholes bellman-ford | SPFA
- POJ 3259 Wormholes(判断负权回路|SPFA||Bellman-Ford)
- POJ - 3259 Wormholes(判断负环, Bellman Ford,SPFA)
- POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)
- POJ 3259 Wormholes(判断负环&(Bellman-Ford|SPFA))
- POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题)
- POJ 3259-Wormholes(Bellman Ford或SPFA判负权值回路)
- Bellman-Ford || SPFA :Wormholes
- POJ 3259 Wormholes(bellman-ford)
- POJ 3259 Wormholes Bellman-Ford
- poj 3259 Wormholes(Bellman-Ford)
- POJ 3259 Wormholes (Bellman-ford)
- codeforces 556A Case of the Zeros and Ones
- LeetCode----Permutation Sequence
- java中form以post、get方式提交数据中文乱码问题总结
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解
- ubuntu下手动安装python源码
- Bellman-Ford||SPFA-POJ-3259-Wormholes
- Javascript--工厂模式、构造函数、原型
- Java中throws和throw的区别讲解
- android学习之1:拍照保存图片
- 关于自己
- 第7周项目3-负数把正数赶出队列
- 详解Tomact中的web.xml
- LinearLayout
- Oracle-序列、索引