hdu 1599 find the mincost route(无向图最小环,floyd)
来源:互联网 发布:网络电视怎么放音乐 编辑:程序博客网 时间:2024/05/22 07:59
find the mincost route
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1996 Accepted Submission(s): 797
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.无向图最小环http://blog.sina.com.cn/s/blog_476a25110100mag6.htmlAC代码:#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>#define ll long long#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)using namespace std;const int maxn = 205;const int maxm = 1000005;const int INF = 1e8; //INF不要设太大int n, m, ans;int G[maxn][maxn], dp[maxn][maxn];void floyd(){ for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[i][j] = G[i][j]; for(int k = 1; k <= n; k++) { for(int i = 1; i < k; i++) for(int j = i + 1; j < k; j++) ans = min(ans, G[i][k] + G[k][j] + dp[i][j]); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(dp[i][j] > dp[i][k] + dp[k][j]) dp[i][j] = dp[i][k] + dp[k][j]; }}int main(){ int a, b, c; while(~scanf("%d%d", &n, &m)) { for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) G[i][j] = i == j ? 0 : INF; while(m--) { scanf("%d%d%d", &a, &b, &c); if(G[a][b] > c) G[a][b] = G[b][a] = c; } ans = INF; floyd(); 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)
- 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算法,最小环无向图)
- HDU 1599 find the mincost route(Floyd无向图最小环)
- hdu 1599 find the mincost route(找无向图最小环)
- HDU 1599 find the mincost route(无向图最小环)
- HDU 1599 find the mincost route(无向图最小环)
- HDU 1599 find the mincost route(Floyd最小环)
- hdu1599 find the mincost route(floyd求无向图最小环)
- HDU-1599-find the mincost route【最短路】【无向图最小环】
- HDU 1599 find the mincost route 无向图最小环
- HDU 1599 find the mincost route 无向图最小环
- HDU 1599 find the mincost route(无向图最小环)
- HDU 1599 find the mincost route Floyd求最小环
- hadoop 日志存放位置总结
- 双系统(windows8和Ubuntu12.04)启动时出现grub rescue
- java基础学习记录之利用数组查表法进行进制转换优化的学习与练习七(十转二、十转八、十转十六)
- js或jquery实现页面打印(局部打印)
- yaffs2文件系统介绍
- hdu 1599 find the mincost route(无向图最小环,floyd)
- oracle导入导出
- vm共享到windows
- 美女CEO自杀网上交易金币压力巨大
- 指针和数组
- sublime 使用
- 算法导论(第三版)15.1-5(斐波那契数列问题)
- 传统百货扎堆试水O2O
- 黑马程序员01_多线程