题目链接
Description

There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is necessary to find the shortest route which begins and ends at the same place. Your task is to write a program which finds such a route.

In the town there are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y_1, …, y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, the road y_k connects crossing points x_k and x_1. All the numbers x_1,…,x_k should be different.The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L(y_1)+L(y_2)+…+L(y_k) where L(y_i) is the length of the road y_i (1<=i<=k). Your program has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible,because there is no sightseeing route in the town.
Input

The first line of input contains two positive integers: the number of crossing points N<=100 and the number of roads M<=10000. Each of the next M lines describes one road. It contains 3 positive integers: the number of its first crossing point, the number of the second one, and the length of the road (a positive integer less than 500).
Output

There is only one line in output. It contains either a string ‘No solution.’ in case there isn’t any sightseeing route, or it contains the numbers of all crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x_1 to x_k from our definition of a sightseeing route), separated by single spaces. If there are multiple sightseeing routes of the minimal length, you can output any one of them.
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

floyd的k的含义,当外层循环执行到k时,g[i][j] 保存的是经过编号不超过k-1,从i到j的最小值。
思路:
1.如何求最小环的大小:
d[i][j]表示初始距离 ,g[i][i] = g[i][j] + d[j][k] + d[k][i];其中i,j都严格小于k

2.如何记录路径
p[i][j] 表示从点i到j所经过的最大点的值,所有可以采用分治的思想,递归求路径

ac代码:

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 105;
int d[N][N],g[N][N];
int p[N][N];
int n,m;//d[i][j]表示初始距离
//如何求最小环的大小,g[i][i] = g[i][j] + d[j][k] + d[k][i];
//其中i,j都严格小于k //如何记录路径
//p[i][j] 表示从点i到j所经过的最大点的值,所有可以采用分治的思想,递归求路径 vector<int> v;
//左闭右开
void get_path(int x,int y){//如果中间不存在点,return if(p[x][y] == 0){v.push_back(x);return;}int k = p[x][y];get_path(x,k);get_path(k,y);return;
}int ans = inf;
void floyd(){int i,j,k;memcpy(d,g,sizeof g);for(k = 1;k<=n;k++){//求最小环for(i = 1;i<k;i++){for(j = i+1;j<k;j++){//i能等于j吗 if((long long)g[i][j] + d[j][k] + d[k][i] < ans){ans = g[i][j] + d[j][k] + d[k][i];v.clear();get_path(i,j);v.push_back(j);v.push_back(k);}}}for(i = 1;i<=n;i++){for(j = 1;j<=n;j++){if(g[i][j] > g[i][k]+g[k][j]){g[i][j] = g[i][k] + g[k][j];p[i][j] = k;}}}} }int main(){int i,j;cin>>n>>m;memset(g,0x3f,sizeof g);for(i =1 ;i<=n;i++) g[i][i] = 0;for(i = 1;i<=m;i++){int a,b,c;cin>>a>>b>>c;g[a][b] = g[b][a] = min(g[a][b],c);}floyd();if(ans == inf){cout<<"No solution."<<endl;}else{for(i = 0;i<v.size();i++)cout<<v[i]<<" ";}return 0;
}

floyd求最小环——poj1734Sightseeing trip相关推荐

  1. 多源最短路径Floyd、Floyd求最小环【模板】

    Floyd算法:用来找出每对点之间的最短距离.图可以是无向图,也可以是有向图,边权可为正,也可以为负,唯一要求是不能有负环.  1.初始化:将Map[][]中的数据复制到Dist[][]中作为每对顶点 ...

  2. 2017百度之星程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】...

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  3. floyd求最小环 模板

    http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...

  4. 最小环 floyd java_干货|Floyd求最小环(CF Shortest Cycle)

    作者:Water_Fox 来源:牛客网 You are given nn integer numbers a1,a2,-,ana1,a2,-,an. Consider graph on nn node ...

  5. hdu 1599(Floyd求最小环)

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

  6. POJ1734 Sightseeing trip floyd求最小环问题

    问题描述 m个点, m条边, 求图中存在的路径最小的环 样例 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 Sa ...

  7. POJ1734(floyd求最小环的路径)

    题目:Sightseeing trip 题意:求一个图中最小环,输出路径. #include <iostream> #include <string.h> #include & ...

  8. hdu 1599 find the mincost route(找无向图最小环)(floyd求最小环)

    ps(我到今天才知道Floyd的核心思想是动态规划==) hdu 1599 find the mincost route(找无向图最小环) 注意!这里写成   #define data 0x3f3f3 ...

  9. Acwing 344.观光之旅(Floyd求最小环)

    Acwing 344.观光之旅 题意 给定一张无向图,求图中一个至少包含 3 个点的环,环上的节点不重复,并且环上的边的长度之和最小. 该问题称为无向图的最小环问题. 你需要输出最小环的方案,若最小环 ...

最新文章

  1. 网络传输模型(概念)
  2. Tomcat发布Maven项目遇到异常:java.lang.OutOfMemoryError: PermGen space
  3. @property与@synthesize的差别
  4. 趣味编程:C#中Specification模式的实现
  5. 《The Corporate Startup》作者访谈
  6. MSP, CMP傻傻分不清楚?一文读懂云管理的春天
  7. SQL中char、varchar、nvarchar、text 的区别
  8. 界面开发用qt还是java,做windows界面,用QT还是MFC?
  9. html网页设计优秀作品和代码,从优秀的网页设计作品中学排版和配色
  10. 如何在官网下载tomcat
  11. Alist云盘视频加密助手:支持云盘视频文件加密与在线播放,不用再担心视频文件被和谐了!
  12. AutodeskCAD卸载后无法再次安装?完美解决!
  13. halcon相机标定及畸变矫正
  14. 【java】简单练习-打印斜坡
  15. LeetCode 912. 排序数组【模板题】
  16. Android 10 去掉系统默认谷歌输入法
  17. 苹果应用内评分弹框次数限制
  18. TCP/IP协议学习( 三 ) ---- ping原理 和 ICMP
  19. nodejs时间函数
  20. JavaScript交互式网页设计 • 【第6章 初识jQuery】

热门文章

  1. 200 port command successful. consider using pasv 425 failed to establish connection
  2. FatFs R0.14 - FF_USE_LFN
  3. 计算机网络的广泛应用对媒体技术,计算机网络中多媒体技术的应用
  4. 【Modelsim常见问题】Can‘t launch the ModelSim-Altera software
  5. Word中如何断开表格中线段
  6. 《C++23》——起草
  7. 谁说的!电波拉皮后反弹更显老,电波拉皮停后的副作用让我一下子蒙蔽了脑袋
  8. img src请求后台值值能判断_删除src值为空的img标签
  9. 【转】不归零码(NRZ),不归零码(NRZ)是什么意思
  10. D. Boboniu Chats with Du (664 div2 贪心 枚举)