1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9. const int maxn = + ;
  10. const int inf = 0x3f3f3f3f;
  11. int n, m, ans;
  12. int mp[maxn][maxn], dis[maxn][maxn], pos[maxn][maxn];
  13. vector<int> path;    //记录路径
  14. inline void getpath( int x, int y ){
  15. if( pos[x][y]== ) return;
  16. getpath( x, pos[x][y] );
  17. path.push_back(pos[x][y]);
  18. getpath( pos[x][y], y );
  19. }
  20.  
  21. inline void floyd(){
  22. ans = inf;
  23. for( int k=; k<=n; k++ ){
  24. for( int i=; i<k; i++ )
  25. for( int j=i+; j<k; j++ )
  26. if( (ll)dis[i][j] + mp[j][k] + mp[k][i] < ans ){
  27. ans = dis[i][j]+mp[j][k]+mp[k][i];  
  28. path.clear();      //发现更优起点,清除之前路径
  29. path.push_back(i);   //该行到32行 按顺序插入才能构成环
  30. getpath( i, j );
  31. path.push_back(j);
  32. path.push_back(k);
  33. }
  34. for( int i=; i<=n; i++ )
  35. for( int j=; j<=n; j++ )
  36. if( dis[i][j] > dis[i][k]+dis[k][j] ){  //此处要按dis[i][k]+dis[k][j]更新最短距离 而不是mp[i][k]+mp[k][j]
  37. dis[i][j] = dis[i][k]+dis[k][j];
  38. pos[i][j] = k;
  39. }
  40. }
  41. }
  42.  
  43. int main(){
  44. scanf("%d%d", &n, &m);
  45. memset( mp, inf, sizeof(mp) );
  46. memset( pos, , sizeof(pos) );
  47. for( int i=; i<=n; i++ ) mp[i][i] = ;
  48. for( int i=; i<m; i++ ){
  49. int u, v, w;
  50. scanf("%d%d%d", &u, &v, &w);
  51. if( w<mp[u][v] ) mp[u][v] = mp[v][u] = w;
  52. }
  53. memcpy( dis, mp, sizeof(mp) );
  54. floyd();
  55. if( ans==inf ){
  56. puts("No solution.");
  57. return ;
  58. }
  59. for( int i=; i<path.size(); i++ )
  60. printf("%d ", path[i]);
  61. puts("");
  62.  
  63. return ;
  64. }
  65.  
  66. /*
  67. Sample Input
  68. 5 7
  69. 1 4 1
  70. 1 3 300
  71. 3 1 10
  72. 1 2 16
  73. 2 3 100
  74. 2 5 15
  75. 5 3 20
  76.  
  77. Sample Output
  78. 1 3 5 2
  79. */

poj1734 Sightseeing trip(Floyd求无向图最小环)的更多相关文章

  1. FZU 2090 旅行社的烦恼 floyd 求无向图最小环

    题目链接:旅行社的烦恼 题意是求无向图的最小环,如果有的话,输出个数,并且输出权值. 刚刚补了一发floyd 动态规划原理,用了滑动数组的思想.所以,这个题就是floyd思想的变形.在k从1到n的过程 ...

  2. #10072. 「一本通 3.2 例 1」Sightseeing Trip(floyd求最小环+路径)

    https://loj.ac/problem/10072 针对无向图 因为Floyd是按照结点的顺序更新最短路的,所以我们在更新最短路之前先找到一个连接点k,当前的点k肯定不存在于已存在的最短路f[i ...

  3. hdu 1599 find the mincost route floyd求无向图最小环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. POJ1734 Sightseeing trip (Floyd求最小环)

    学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...

  5. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

  6. POJ1734 - Sightseeing trip

    DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...

  7. poj1734 Sightseeing trip[最小环]

    一个最小环裸题.最小环的两种求法dijkstra和Floyd直接参见这里我就是从这里学的,不想写了. 注意这里最重要的一个点是利用了Floyd的dp过程中路径上点不超过$k$这一性质,来枚举环上最大编 ...

  8. POJ 1734 Sightseeing trip(Floyd)

    题目传送门 题目中文翻译: Description 桑给巴尔岛上的阿德尔顿镇有一家旅行社,它已决定为其客户提供除了许多其他名胜之外的景点.为了尽可能地从景点赚取收入,该机构已经接受了一个精明的决定:有 ...

  9. bzoj 1027 floyd求有向图最小环

    结合得好巧妙.... 化简后的问题是: 给你两个点集A,B,求B的一个子集BB,使得BB的凸包包含A的凸包,求BB的最小大小. 先特判答案为1,2的情况,答案为3的情况,我们先构造一个有向图: 对于B ...

随机推荐

  1. js小数计算的问题,为什么0.1+0.2 != 0.3

    //下面可以用原生解决 0.1+0.2 的问题 parseFloat((0.1 + 0.2).toFixed(10)) 复制代码 console.log(0.1+0.2===0.3); //true ...

  2. 【转】Centos下编译升级安装Boost

    https://www.xingchenw.cn/article/191 Centos下编译升级安装Boost 首先在官网现在相应的包 https://www.boost.org/users/down ...

  3. Python的编码规范

    7. 什么是 PEP8? 8号Python增强提案,是针对Python代码格式而编写的风格指南 8. 了解 Python 之禅么? 通过 import this 语句可以获取其具体的内容.它告诉大家何 ...

  4. ReetrantLock架构源码 --- One

    以下是绅士通过processon画的一个比较简单的架构,模板模式理清楚确实需要一点点时间 Doug Lea牛ban- .- 最近在复习整理知识点,这上面的一些关键方法addWaiter();acqui ...

  5. fineui 实现下拉框模糊查询

    官网下拉框模糊查询只能实现首字母模糊匹配,如果实现类似这样的 like '%'+关键字+'%',却没有. 今天群里的没想好同学分享了,前后模糊匹配代码.   代码示例: <body>   ...

  6. 第3课,python使用for循环

    前言: 学习了python的while循环后感觉循环是挺强大的.下面学习一个更智能,更强大的循环-- for循环. 课程内容: 1.由while循环,到for循环,格式和注意项 2.for循环来报数 ...

  7. sublime text 疑难解决

    sublime text 白色边框方框解决方法 https://blog.csdn.net/weixin_43228019/article/details/82766316 Sublime Text提 ...

  8. 关于一致性hash,这可能是全网最形象生动最容易理解的文档,想做架构师的你来了解一下

    问题提出 一致性hash是什么?假设有4台缓存服务器N0,N1,N2,N3,现在需要存储数据OBJECT1,OBJECT2,OBJECT3,OBJECT4,OBJECT5,OBJECT5,OBJECT ...

  9. C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)

    之前在上C++的课的时候,印象中有那么一句话:如果一个类没有任何构造函数,那么编译器会生成一个默认的构造函数 今天在看<深度探索C++对象模型>的第二章:“构造函数语意学”的时候发现之前听 ...

  10. System.Web.NullPointerException

    在.Net异步webApi中我们需要记录日志信息,需要获取客户端的ip地址,我们需要使用:HttpContext.Current.Request.ServerVariables["REMOT ...