码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-03 23:30:41      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

  • 时间: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. }




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

标签:

原文地址:http://www.cnblogs.com/qhy285571052/p/7fdb3b4b112dfcb00e35afb4c51bec2d.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!