find the mincost route(城市最小环)
来源:互联网 发布:奥地利 知乎 编辑:程序博客网 时间:2024/06/02 01:30
find the mincost route(城市最小环)
杭州有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 31 2 12 3 11 3 13 31 2 11 2 32 3 1
Sample Output
3It's impossible.
#include<iostream>#include<cstdio>const int N=111;const int inf=1000000;#define min(x,y) ((x<y)?(x):(y));using namespace std; int dist[N][N]; int edge[N][N]; int n,m; void floyd() { int ans=inf; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { dist[i][j]=edge[i][j]; } } //根据Floyed的原理,在最外层循环做了k-1次之后,dis[i][j]则代表了i到j的路径中所有结点编号都小于k的最短路径 for(int k=1;k<=n;k++) { //环的最小长度为edge[i][k]+edge[k][j]+i->j的路径中所有编号小于k的最短路径长度 for(int i=1;i<k;i++) { for(int j=i+1;j<k;j++) { if(dist[i][j]+edge[i][k]+edge[k][j]<inf) { ans=min(ans,dist[i][j]+edge[i][k]+edge[k][j]); } } } //floyd原来的部分,更新dist[i][j]/// for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(dist[i][j]>dist[i][k]+dist[k][j]) { dist[i][j]=dist[i][k]+dist[k][j]; } } } } if(ans==inf) { printf("It's impossible.\n"); }else printf("%d\n",ans); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) { edge[i][j]=0; } else edge[i][j]=inf; } } int x,y,cost; for(int i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&cost); //考虑重边 if(edge[x][y]>cost) { edge[x][y]=edge[y][x]=cost; } } floyd(); } return 0; }
阅读全文
1 0
- find the mincost route(城市最小环)
- find the mincost route(城市最小环)
- HDU 1599 find the mincost route(Floyd最小环)
- HDOJ--1599--find the mincost route(floyd+最小环)
- hdu 1599 find the mincost route(最小环)
- HDU1599 find the mincost route (floyd求最小环)
- hdu1599—find the mincost route(floyed最小环)
- hdu-1599-find the mincost route(弗洛伊德 最小环)
- HDU1599 find the mincost route 无向图最小环
- HDU 1599 find the mincost route Floyd求最小环
- hdu1599 find the mincost route floyd求最小环
- HDU1599 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判最小环)
- A+B problem
- 2-6使用ACL访问控制列表对文件的访问
- 2017.6.4测试 题七 K上升段
- 常见的排序算法C语言实现(—)
- 理解Linux文件权限
- find the mincost route(城市最小环)
- HDU 6090 Rikka with Graph【思维题】
- 芳姐和芳姐的猪
- Centos7.3 LNMP+phpMyAdmin 最新环境搭建
- SpringMVC 自动绑定数据
- C++知识总结(5)
- NYOJ 467 中缀式变后缀式
- 产品经理该如何与设计师沟通?
- 1831 小C的游戏(博弈、规律)