find the mincost route
来源:互联网 发布:mac brave 编辑:程序博客网 时间:2024/05/22 14:24
find the mincost route
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3842 Accepted Submission(s): 1546
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.
Author
8600
Source
HDU 2007-Spring Programming Contest - Warm Up (1)
Recommend
8600 | We have carefully selected several similar problems for you: 1595 1598 1596 1142 1217
这个题用Floyd来写需要设置两个二维数组,一个用来存原始的边,另一个用来存Floyd不断更新后的两点之间的当前最短距离,其实求最小环就是在更新过程中,加入一个二重循环判断是否存在一个之前没用过的点k能使i,j联通,并不断寻找满足条件的最小值。
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define N 109#define inf 2147483647/3using namespace std;int n,m,k,ans;int maze[N][N],dist[N][N];void floyd(){ int i,j,k; ans = inf; for(k=1;k <= n;k++) { for(i=1;i < k;i++) for(j=i+1;j < k;j++) if(ans > dist[i][j] + maze[i][k] + maze[j][k]) { ans = dist[i][j] + maze[i][k] + maze[j][k]; } for(i=1;i <= n;i++) for(j=1;j <= n;j++) if(i-k && j-k && i-j && dist[i][j] > dist[i][k]+dist[k][j]) { dist[i][j] = dist[i][k]+dist[k][j]; } }}int main(){ int i,j,st,en,len; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i <= n;i++) for(j=1;j <= n;j++) { maze[i][j] = dist[i][j] = inf; } while(m--) { scanf("%d%d%d",&st,&en,&len); if(st-en && dist[st][en] > len) dist[st][en]=dist[en][st]=maze[st][en]=maze[en][st]=len; } floyd(); if(ans == inf) printf("It's impossible.\n"); else printf("%d\n",ans); } return 0;}
0 0
- find the mincost route
- find the mincost route
- find the mincost route
- find the mincost route
- find the mincost route
- find the mincost route
- find the mincost route
- HDU1599 find the mincost route
- hdu1599 find the mincost route
- hdu1599 find the mincost route
- hdu1599 find the mincost route
- Hdoj find the mincost route
- find the mincost route hdoj1599
- HDU1599:find the mincost route
- HDU_1599 find the mincost route
- find the mincost route HDU
- hdu1599 find the mincost route
- [hdu1599]find the mincost route
- brew update 慢 解决办法 镜像更新源
- 行人检测资源汇总
- Qt之无边框窗口创建
- Android Studio下JNI开发
- 解决 Make sure the Cursor is initialized correctly before accessing data from it
- find the mincost route
- 【Code Art / C#】随机贝塞尔曲线效果测试
- 139. Word Break | Java最短代码实现
- CentOs 6.6配置 Apache服务器 Mysql5.5 PHP5.5 以及Tomcat8.0 JDK的方法
- [扫描线 树链剖分 树状数组] BZOJ2758 [SCOI2012]Blinker的噩梦
- 为 Notepad++ 安装 NppFTP 插件,查看修改虚拟机上的文本文件
- HTML入门语法(1)
- 2015春季练习——一道水题的思考
- java.lang.IndexOutOfBoundsException 错误解决