poj1734 Sightseeing trip(Floyd求无向图最小环)
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- using namespace std;
- typedef long long ll;
- const int maxn = + ;
- const int inf = 0x3f3f3f3f;
- int n, m, ans;
- int mp[maxn][maxn], dis[maxn][maxn], pos[maxn][maxn];
- vector<int> path; //记录路径
- inline void getpath( int x, int y ){
- if( pos[x][y]== ) return;
- getpath( x, pos[x][y] );
- path.push_back(pos[x][y]);
- getpath( pos[x][y], y );
- }
- inline void floyd(){
- ans = inf;
- for( int k=; k<=n; k++ ){
- for( int i=; i<k; i++ )
- for( int j=i+; j<k; j++ )
- if( (ll)dis[i][j] + mp[j][k] + mp[k][i] < ans ){
- ans = dis[i][j]+mp[j][k]+mp[k][i];
- path.clear(); //发现更优起点,清除之前路径
- path.push_back(i); //该行到32行 按顺序插入才能构成环
- getpath( i, j );
- path.push_back(j);
- path.push_back(k);
- }
- for( int i=; i<=n; i++ )
- for( int j=; j<=n; j++ )
- if( dis[i][j] > dis[i][k]+dis[k][j] ){ //此处要按dis[i][k]+dis[k][j]更新最短距离 而不是mp[i][k]+mp[k][j]
- dis[i][j] = dis[i][k]+dis[k][j];
- pos[i][j] = k;
- }
- }
- }
- int main(){
- scanf("%d%d", &n, &m);
- memset( mp, inf, sizeof(mp) );
- memset( pos, , sizeof(pos) );
- for( int i=; i<=n; i++ ) mp[i][i] = ;
- for( int i=; i<m; i++ ){
- int u, v, w;
- scanf("%d%d%d", &u, &v, &w);
- if( w<mp[u][v] ) mp[u][v] = mp[v][u] = w;
- }
- memcpy( dis, mp, sizeof(mp) );
- floyd();
- if( ans==inf ){
- puts("No solution.");
- return ;
- }
- for( int i=; i<path.size(); i++ )
- printf("%d ", path[i]);
- puts("");
- return ;
- }
- /*
- Sample Input
- 5 7
- 1 4 1
- 1 3 300
- 3 1 10
- 1 2 16
- 2 3 100
- 2 5 15
- 5 3 20
- Sample Output
- 1 3 5 2
- */
poj1734 Sightseeing trip(Floyd求无向图最小环)的更多相关文章
- FZU 2090 旅行社的烦恼 floyd 求无向图最小环
题目链接:旅行社的烦恼 题意是求无向图的最小环,如果有的话,输出个数,并且输出权值. 刚刚补了一发floyd 动态规划原理,用了滑动数组的思想.所以,这个题就是floyd思想的变形.在k从1到n的过程 ...
- #10072. 「一本通 3.2 例 1」Sightseeing Trip(floyd求最小环+路径)
https://loj.ac/problem/10072 针对无向图 因为Floyd是按照结点的顺序更新最短路的,所以我们在更新最短路之前先找到一个连接点k,当前的点k肯定不存在于已存在的最短路f[i ...
- 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 ...
- POJ1734 Sightseeing trip (Floyd求最小环)
学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...
- poj1734 Sightseeing trip【最小环】
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions:8588 Accepted:3224 ...
- POJ1734 - Sightseeing trip
DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...
- poj1734 Sightseeing trip[最小环]
一个最小环裸题.最小环的两种求法dijkstra和Floyd直接参见这里我就是从这里学的,不想写了. 注意这里最重要的一个点是利用了Floyd的dp过程中路径上点不超过$k$这一性质,来枚举环上最大编 ...
- POJ 1734 Sightseeing trip(Floyd)
题目传送门 题目中文翻译: Description 桑给巴尔岛上的阿德尔顿镇有一家旅行社,它已决定为其客户提供除了许多其他名胜之外的景点.为了尽可能地从景点赚取收入,该机构已经接受了一个精明的决定:有 ...
- bzoj 1027 floyd求有向图最小环
结合得好巧妙.... 化简后的问题是: 给你两个点集A,B,求B的一个子集BB,使得BB的凸包包含A的凸包,求BB的最小大小. 先特判答案为1,2的情况,答案为3的情况,我们先构造一个有向图: 对于B ...
随机推荐
- 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 ...
- 【转】Centos下编译升级安装Boost
https://www.xingchenw.cn/article/191 Centos下编译升级安装Boost 首先在官网现在相应的包 https://www.boost.org/users/down ...
- Python的编码规范
7. 什么是 PEP8? 8号Python增强提案,是针对Python代码格式而编写的风格指南 8. 了解 Python 之禅么? 通过 import this 语句可以获取其具体的内容.它告诉大家何 ...
- ReetrantLock架构源码 --- One
以下是绅士通过processon画的一个比较简单的架构,模板模式理清楚确实需要一点点时间 Doug Lea牛ban- .- 最近在复习整理知识点,这上面的一些关键方法addWaiter();acqui ...
- fineui 实现下拉框模糊查询
官网下拉框模糊查询只能实现首字母模糊匹配,如果实现类似这样的 like '%'+关键字+'%',却没有. 今天群里的没想好同学分享了,前后模糊匹配代码. 代码示例: <body> ...
- 第3课,python使用for循环
前言: 学习了python的while循环后感觉循环是挺强大的.下面学习一个更智能,更强大的循环-- for循环. 课程内容: 1.由while循环,到for循环,格式和注意项 2.for循环来报数 ...
- sublime text 疑难解决
sublime text 白色边框方框解决方法 https://blog.csdn.net/weixin_43228019/article/details/82766316 Sublime Text提 ...
- 关于一致性hash,这可能是全网最形象生动最容易理解的文档,想做架构师的你来了解一下
问题提出 一致性hash是什么?假设有4台缓存服务器N0,N1,N2,N3,现在需要存储数据OBJECT1,OBJECT2,OBJECT3,OBJECT4,OBJECT5,OBJECT5,OBJECT ...
- C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)
之前在上C++的课的时候,印象中有那么一句话:如果一个类没有任何构造函数,那么编译器会生成一个默认的构造函数 今天在看<深度探索C++对象模型>的第二章:“构造函数语意学”的时候发现之前听 ...
- System.Web.NullPointerException
在.Net异步webApi中我们需要记录日志信息,需要获取客户端的ip地址,我们需要使用:HttpContext.Current.Request.ServerVariables["REMOT ...