HDU-1599 find the mincost route (无向图最小环[Floyd])
来源:互联网 发布:秦可卿怎么死的知乎 编辑:程序博客网 时间:2024/05/18 03:55
find the mincost route
http://acm.hdu.edu.cn/showproblem.php?pid=1599
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)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.
大致思路:无向图的环最少有三个点,所以需要增加一部分求最小环;枚举中间点k,在用其更新最短路前,先找最小环,令1<=i<j<k,即k点必定不在i,j的最短路上,则这个环中至少有三个点,这个环的权值为:dis[i][j]+g[i][k]+g[k][j]
没仔细看样例,会存在重边,结果WA了
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dis[105][105],g[105][105],n,m,ans;void floyd() { ans=0x1f1f1f1f; 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,dis[i][j]+g[i][k]+g[k][j]); 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]); }}int main() { int s,e,w; while(scanf("%d%d",&n,&m)==2) { memset(g,0x1f,sizeof(g)); memset(dis,0x1f,sizeof(dis)); while(m-->0) { scanf("%d%d%d",&s,&e,&w); if(w<g[s][e]) dis[s][e]=dis[e][s]=g[s][e]=g[e][s]=w; } floyd(); if(ans==0x1f1f1f1f) 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求最小环
- 自己动手搭建 SSM框架——(Spring+SpringMVC+MyBatis)问题总结
- 【贪心】HDU2187悼念512汶川大地震遇难同胞——老人是真饿了
- Object-C 属性
- Java 关闭Thread详解
- jumbo安装redis步骤以及redis配置详解
- HDU-1599 find the mincost route (无向图最小环[Floyd])
- URLDecoder: Incomplete trailing escape (%) pattern错误处理
- I/O多路转接之select
- Java 构造器研究
- 从数据仓库到数据视图
- BestCoder Round #72 Clarke and chemistry
- chrome浏览器安装
- zoj3490
- 判断两个二叉树是否相等(仅结构) -- 递归和非递归实现