HDU 1599 find the mincost route(floyd算法)
来源:互联网 发布:背包装什么 知乎 编辑:程序博客网 时间:2024/05/16 17:06
find the mincost route
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
HDU 1599
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)。
Output
对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出”It’s impossible.”.
Sample Input
3 3
1 2 1
2 3 1
1 3 1
3 3
1 2 1
1 2 3
2 3 1
Sample Output
3
It’s impossible.
不明不白的在三目运算符这里出了鬼,诸事不顺啊~
floyd算法求最小回路问题,还是看了几别人的代码才能领悟~尴尬啊,学了好长时间还是做不出来什么题诶
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define M 110#define INF 0x3f3f3fint map[M][M], dist[M][M];int main(){ int n, m, a, c, b; while(scanf("%d%d", &n, &m) != EOF) { for(int i=1; i<=n; i++)//初始化 { for(int j=1; j<=n; j++) { if(i == j) { map[i][j] = dist[i][j] = 0; } else { map[i][j] = dist[i][j] = INF; } } } for(int i=0; i<m; i++) { scanf("%d%d%d", &a, &b, &c); map[a][b] = map[b][a] = dist[b][a] = dist[a][b] = min(dist[a][b], c); } int ans = INF; for(int k=1; k<=n; k++) { for(int i=1; i<k; i++)//未插入 k 点时的最小环 { for(int j=i+1; j<k; j++) { ans = min(ans, dist[i][j] + map[j][k] + map[k][i]);//一个回路,从出发到回来,会有那么一个点在最后连接着,我们就把那个点提出来,找出最短回路,所以两个数组就用在了这里 } } for(int i=1; i<=n; i++)//将 k 点插入,求最短路 { for(int j=1; j<=n; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } if(ans == INF) { printf("It's impossible.\n"); } else { printf("%d\n", ans); } } return 0;}
0 0
- HDU 1599 find the mincost route(floyd算法)
- HDU 1599 find the mincost route (floyd)
- find the mincost route(floyd算法)
- HDU 1599 find the mincost route(Floyd最小环)
- hdu 1599 find the mincost route(Floyd)
- hdu 1599 find the mincost route (最小环与floyd算法)
- HDU 1599 find the mincost route Floyd求最小环
- hdu 1599 find the mincost route floyd求最小环
- HDU 1599 find the mincost route (Floyd 最小环)
- HDU - 1599 find the mincost route(Floyd求最小环)
- hdu 1599 find the mincost route(floyd 最小环)
- HDU 1599 find the mincost route(Floyd判最小环)
- hdu 1599 find the mincost route【floyd求最小环】
- hdu 1599 find the mincost route 求最小环floyd
- hdu 1599find the mincost route(Floyd算法,最小环无向图)
- HDOJ--1599--find the mincost route(floyd+最小环)
- hd 1599 find the mincost route(floyd)
- HDU 1599 find the mincost route (无向图floyd最小环详解)
- 使用java代码来实现dvd管理系统
- Android进阶系列3—再说View的事件分发
- Pro Android学习笔记(一四七):拖拽(1):通过MotionEvent来实现
- ActiveMQ消息队列的应用 C#客户端 Web后端
- placeholder颜色
- HDU 1599 find the mincost route(floyd算法)
- Android中HttpURLConnection获取网址源码,子线程里更新UI
- MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 main,该符号在函数 __tmainCRTStartup 中被引用
- Android:视频播放器相关问题汇总(草稿)
- 3361数据结构实验之图论四:迷宫探索
- QT中的随机函数的用法
- 如何在Mac上安装nginx以及所需模块
- 记一次面试(被骗)经历
- sql保留字符串中的某个字符前面的字符串