HDU 1599:find the mincost route(floyd最短路成环)
来源:互联网 发布:星你同人之我知你不知 编辑:程序博客网 时间:2024/06/02 04:02
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法求最短路成环的模板题,要注意的是只需要成环就好了,不必要一定从1开始
代码(C)
#include<stdio.h>#include<string.h>#define inf 99999999#define Min(a,b)((a)<(b))?(a):(b)int n,m,min;int d[102][102];int a[102][102];void floyd(){ int i,k,j; min=inf; for(k=1;k<=n;k++) { for(i=1;i<k;i++) for(j=1;j<i;j++) min=Min(min,d[i][j]+a[i][k]+a[k][j]);//判断成环最小值 for(i=1;i<=n;i++) for(j=1;j<=n;j++) d[i][j]=Min(d[i][j],d[i][k]+d[k][j]);//原始floyd算法核心 }}int main(){ int p,q,i,j,len; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) a[i][j]=inf; for(i=1;i<=m;i++) { scanf("%d%d%d",&p,&q,&len); if(len<a[p][q]) a[p][q]=a[q][p]=len; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) d[i][j]=a[i][j]; floyd(); if(min!=inf) printf("%d\n",min); else printf("It's impossible.\n"); } return 0;}
0 0
- HDU 1599:find the mincost route(floyd最短路成环)
- HDU 1599 find the mincost route(Floyd最小环)
- hdoj 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)
- HDOJ--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求最小环) >>
- C++原码、反码、补码详解
- 最近遇到的一些问题总结
- 不管是Wifi 、还是 4G 下都可以获取公网IP地址
- linux系统644、755、777权限详解
- 【CentOS】JDK1.7的安装与配置
- HDU 1599:find the mincost route(floyd最短路成环)
- 分析Redis架构设计
- hibernate的使用注意点
- 文章标题 codeforces 115A:Party ( 并查集)
- WebService之WADL和WSDL ING
- 45.实现自定义View拖拽效果的5种方法
- zookeeper作用
- netty学习02-几个核心类以及其介绍
- android的Lambda