floyd求图的最小环路 HDU1599
来源:互联网 发布:生活半径 知乎 编辑:程序博客网 时间:2024/05/15 00:40
http://acm.split.hdu.edu.cn/showproblem.php?pid=1599
find the mincost route
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5879 Accepted Submission(s): 2346
Problem Description
杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区。现在8600需要你帮他找一条这样的路线,并且花费越少越好。
Input
第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
Output
对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出"It's impossible.".
Sample Input
3 31 2 12 3 11 3 13 31 2 11 2 32 3 1
Sample Output
3It's impossible.
floyd求图的最小环模板题。时间复杂度O(n^3)
特别注意:INF不要太大,保证相加不爆精度。第25行循环j从i+1开始
【代码】:
#include<iostream>using namespace std;const int INF=0x1f3f3f3f;int Map[105][105];int dis[105][105];int n,m;int main(){ while(cin>>n>>m) { for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) Map[i][j]=dis[i][j]=INF; while(m--) { int u,v,len; cin>>u>>v>>len; if(len<dis[u][v]) Map[u][v]=Map[v][u]=dis[u][v]=dis[v][u]=len; } int ans=INF; for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) ans=min(ans,dis[i][j]+Map[j][k]+Map[k][i]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } if(ans==INF)cout<<"It's impossible."<<endl; else cout<<ans<<endl; }}
阅读全文
0 0
- floyd求图的最小环路 HDU1599
- hdu1599 floyd 求最小环
- hdu1599 Floyd求最大环 最小环
- hdu1599 find the mincost route(floyd求无向图最小环)
- HDU1599:find the mincost route(Floyd最小无向环)
- hdu1599求无向图的最小环
- hdu1599 find the mincost route floyd求最小环
- HDU1599 find the mincost route(floyd求最小环)
- Floyd求最小环 hdu1599 find the mincost route
- HDU1599 find the mincost route (floyd求最小环)
- Floyd求字典序最小的路径
- hdu1599+floyd最小环
- 无向图求环路
- uva 11090 Going in Cycle!! (求平均权值最小的环路)
- hdu1599(floyd算法讲解)
- 最小环 hdu1599 Floyd算法
- 深度搜索的应用----环路最小路径
- 图的环路问题
- NIO 01 NIO概述
- 简单的音乐网页
- React Native 高德地图组件的使用(react-native-amap3d)
- maven入门
- httpclient_get请求
- floyd求图的最小环路 HDU1599
- ReactNative WebView组件通信
- 前端的调试方法
- 【Unity3d】在Unity3d中使用百度AI人脸识别功能
- ReactNative接入支付宝支付
- 相关性检验–Spearman秩相关系数和皮尔森相关系数
- C语言中的getchar和putchar详解
- SwipeRefreshLayout源码解析
- 将 React Native 项目运行在 Web 浏览器上面