[2016-04-03][POJ][3259][Wormholes]

  • 时间:2016-04-03 20:22:04 星期日

  • 题目编号:[2016-04-03][POJ][3259][Wormholes]

  • 题目大意:某农场有n个节点,m条边(双向)和w个虫洞(单向),(走虫洞可以回到过去的时间),问能否和过去的自己相遇

  • 分析:

    • 题目意思就是给定一个带负权的图,问是否存在负圈,

spfa_bfs

spfa_dfs

bellman_ford

  1. #include <vector>
  2. #include <queue>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cstdio>
  6. using namespace std;
  7. //bfs_spfa
  8. const int maxn = 500 + 10;
  9. struct Edge{
  10. int v,c;
  11. Edge(int _v,int _c):v(_v),c(_c){}
  12. };
  13. vector<Edge> e[maxn];
  14. void addedge(int u,int v,int c){
  15. e[u].push_back(Edge(v,c));
  16. }
  17. bool vis[maxn];
  18. int cnt[maxn],d[maxn];
  19. bool spfa(int s,int n){
  20. memset(vis,0,sizeof(vis));
  21. memset(d,0x3f,sizeof(d));
  22. memset(cnt,0,sizeof(cnt));
  23. vis[s] = cnt[s] = 1;
  24. d[s] = 0;
  25. queue<int> q;
  26. q.push(s);
  27. while(!q.empty()){
  28. int u = q.front();q.pop();
  29. vis[u] = 0;
  30. for(int i = 0 ;i < e[u].size();++i){
  31. int v = e[u][i].v;
  32. if(d[v] > d[u] + e[u][i].c){
  33. d[v] = d[u] + e[u][i].c;
  34. if(!vis[v]){
  35. vis[v] = 1;
  36. q.push(v);
  37. if(++cnt[v] > n)
  38. return false;
  39. }
  40. }
  41. }
  42. }
  43. return true;
  44. }
  45. inline void ini(int n){//每组数据存图记得初始化
  46. for(int i = 0; i <= n ; ++i){
  47. e[i].clear();
  48. }
  49. }
  50. int main(){
  51. int f;
  52. scanf("%d",&f);
  53. while(f--){
  54. int n,m,w;
  55. scanf("%d%d%d",&n,&m,&w);
  56. ini(n);
  57. int s,e,t;
  58. for(int i = 0;i < m ; ++i){
  59. scanf("%d%d%d",&s,&e,&t);
  60. addedge(s,e,t);
  61. addedge(e,s,t);
  62. }
  63. for(int i = 0;i < w;++i){
  64. scanf("%d%d%d",&s,&e,&t);
  65. addedge(s,e,-t);
  66. }
  67. puts(spfa(1,n)?"NO":"YES");
  68. }
  69. return 0;
  70. }
  1. #include <vector>
  2. #include <queue>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cstdio>
  6. using namespace std;
  7. //dfs_spfa
  8. const int maxn = 500 + 10;
  9. struct Edge{
  10. int v,c;
  11. Edge(int _v,int _c):v(_v),c(_c){}
  12. };
  13. vector<Edge> e[maxn];
  14. void addedge(int u,int v,int c){
  15. e[u].push_back(Edge(v,c));
  16. }
  17. bool vis[maxn];
  18. int d[maxn];
  19. bool spfa(int u){
  20. vis[u] = 1;
  21. for(int i = 0 ;i < e[u].size();++i){
  22. int v = e[u][i].v;
  23. if(d[v] > d[u] + e[u][i].c){
  24. d[v] = d[u] + e[u][i].c;
  25. if(vis[v] || spfa(v)){
  26. return 1;
  27. }
  28. }
  29. }
  30. vis[u] = 0;
  31. return 0;
  32. }
  33. inline void ini(int n){
  34. for(int i = 0; i <= n ; ++i){
  35. e[i].clear();
  36. }
  37. memset(vis,0,sizeof(vis));
  38. memset(d,0x3f,sizeof(d));
  39. d[1] = 0;
  40. }
  41. int main(){
  42. int f;
  43. scanf("%d",&f);
  44. while(f--){
  45. int n,m,w;
  46. scanf("%d%d%d",&n,&m,&w);
  47. ini(n);
  48. int s,e,t;
  49. for(int i = 0;i < m ; ++i){
  50. scanf("%d%d%d",&s,&e,&t);
  51. addedge(s,e,t);
  52. addedge(e,s,t);
  53. }
  54. for(int i = 0;i < w;++i){
  55. scanf("%d%d%d",&s,&e,&t);
  56. addedge(s,e,-t);
  57. }
  58. puts(spfa(1)?"YES":"NO");
  59. }
  60. return 0;
  61. }
  1. #include <vector>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. //bellman_ford
  6. const int maxn = 500 + 10;
  7. struct Edge{
  8. int u,v,c;
  9. Edge(int _u,int _v,int _c):u(_u),v(_v),c(_c){}
  10. };
  11. vector<Edge> e;
  12. void addedge(int u,int v,int c){
  13. e.push_back(Edge(u,v,c));
  14. }
  15. int d[maxn];
  16. bool bellman_ford(int s,int n){
  17. memset(d,0x3f,sizeof(d));
  18. d[s] = 0;
  19. for(int i = 1;i < n ; ++i){
  20. bool flg = 0;
  21. for(int j = 0;j < e.size();++j){
  22. int u = e[j].u,v = e[j].v,c = e[j].c;
  23. if(d[v] > d[u] + c){
  24. d[v] = d[u] + c;
  25. flg = 1;
  26. }
  27. }
  28. if(!flg) return 1;//没有负环
  29. }
  30. for(int j = 0;j < e.size();++j){
  31. int u = e[j].u,v = e[j].v,c = e[j].c;
  32. if(d[v] > d[u] + c) return 0;
  33. }
  34. return 1;
  35. }
  36. int main(){
  37. int f;
  38. scanf("%d",&f);
  39. while(f--){
  40. int n,m,w;
  41. scanf("%d%d%d",&n,&m,&w);
  42. e.clear();
  43. int s,e,t;
  44. for(int i = 0;i < m ; ++i){
  45. scanf("%d%d%d",&s,&e,&t);
  46. addedge(s,e,t);
  47. addedge(e,s,t);
  48. }
  49. for(int i = 0;i < w;++i){
  50. scanf("%d%d%d",&s,&e,&t);
  51. addedge(s,e,-t);
  52. }
  53. puts(bellman_ford(1,n)?"NO":"YES");
  54. }
  55. return 0;
  56. }

来自为知笔记(Wiz)

时间: 2024-10-10 08:13:43

[2016-04-03][POJ][3259][Wormholes]的相关文章

POJ 3259 Wormholes SPFA算法题解

本题其实也可以使用SPFA算法来求解的,不过就一个关键点,就是当某个顶点入列的次数超过所有顶点的总数的时候,就可以判断是有负环出现了. SPFA原来也是可以处理负环的. 不过SPFA这种处理负环的方法自然比一般的Bellman Ford算法要慢点了. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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 pa

POJ 3259 Wormholes(SPFA)

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! Eac

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 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 p

POJ 3259 Wormholes Bellman题解

本题就是需要检查有没有负环存在于路径中,使用Bellman Ford算法可以检查是否有负环存在. 算法很简单,就是在Bellman Ford后面增加一个循环判断就可以了. 题目故事很奇怪,小心读题. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const int MAX_W = 201; struct E

POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)

链接:http://poj.org/problem?id=3259 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 BE

Poj 3259 Wormholes 负环判断 SPFA &amp; BellmanFord

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

poj 3259 Wormholes(Bellman-Ford)

poj 3259 Wormholes 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 entere

poj 3259 Wormholes (Bellman-ford)

链接: poj 3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路(双向的),即从a到b和从b到a时间都为c.虫洞的性质:时间倒流.即通过虫洞从a到b所花时间为 -c(单向的).问从某块田出发,他能否通过虫洞的性质回到出发点前 思路:这题实际就是判断是否存在负权回路,可以用SPFA算法或Bellman-Ford算法判断.若存在负权回路,则可以达到目的,否则不可以. Bellman-ford算法 #include<stdio.h> #incl