hdu1599find the mincost route (floyd算法,相对模板而言时间优化了一半)
来源:互联网 发布:移动网络可以玩lol吗 编辑:程序博客网 时间:2024/04/30 12:06
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.#include<stdio.h>#include<queue>#include<iostream>using namespace std;#define INF 99999999int map[105][105],edg[105][105],n,dist;void init(){ for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) map[i][j]=INF;}void floyd(){ dist=INF; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) edg[i][j]=map[i][j]; for(int mid=1;mid<=n;mid++)//以三个不同的点进行查找,更新 for(int i=1;i<=n;i++) if(i!=mid) for(int j=1;j<=n;j++) if(i!=j&&j!=mid) { if(dist>map[mid][i]+edg[i][j]+map[mid][j])//无向时的查找 dist=map[mid][i]+edg[i][j]+map[mid][j]; if(edg[i][j]>edg[i][mid]+edg[mid][j])//以三角形的形式把点i,j连系起来,edg[i][j],从i走到j,中间可能经过很多点,但只要直接到的长度大于中间加起来的路长,就可以替换 edg[i][j]=edg[i][mid]+edg[mid][j]; }}int main(){ int m,a,b,p; while(scanf("%d%d",&n,&m)>0) { init(); while(m--) { scanf("%d%d%d",&a,&b,&p); if(map[a][b]>p&&a!=b) map[a][b]=map[b][a]=p; } floyd(); if(dist!=INF) printf("%d\n",dist); else printf("It's impossible.\n"); }}
- hdu1599find the mincost route (floyd算法,相对模板而言时间优化了一半)
- hdu1599find the mincost route
- hdu1599find the mincost route(floyed)
- find the mincost route(floyd算法)
- HDU 1599 find the mincost route(floyd算法)
- HDU1599:find the mincost route(Floyd)
- floyd寻找最短环find the mincost route
- hdoj1599find the mincost route【floyd+最小环】
- HDU1599 find the mincost route (Floyd)
- HDU 1599 find the mincost route (floyd)
- HDOJ 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 Floyd求最小环
- hdu1599 find the mincost route floyd求最小环
- HDU1599:find the mincost route(Floyd最小无向环)
- HDU1599 find the mincost route(floyd求最小环)
- hdu 1599 find the mincost route floyd求最小环
- C++改写成C需要注意的几点
- hdu3911 Black And White
- 使用 Java 对 Linux 下文件编码格式进行批量转换
- Iterator和for循环的使用
- quick-cocos2d-x开发环境Lua for IntelliJ IDEA的安装
- hdu1599find the mincost route (floyd算法,相对模板而言时间优化了一半)
- codeforces #215 div2前四题
- iPhone 上实现异步加载图片并缓存代码
- object-c 多线程 加锁
- iOS-获取当前时间的年、月、日、时、分、秒
- 分步示意如何在iPhone应用中植入iAd广告
- iOS 开发之EXC_BAD_ACCESS异常分析
- IOS中四种解析JSON的方法(IOS5自带支持方法、JSONKit、SBJson、TouchJson)
- ios非UTF-8格式的网页解析