find the mincost route(floyd算法)
来源:互联网 发布:里杰卡尔德知乎 编辑:程序博客网 时间:2024/05/18 02:43
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.题目数据较小,只有100个景区,1000条路,所以可用floyd算法,INF定义为十万多一点点就可以了#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define INF 100001#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long LL;const int N = 105;const int maxn = 1005;int n,m,ans;int a[N][N],d[N][N];void Floyd(){ int i,k,j; ans=INF; for(k=1; k<=n; k++) { for(i=1; i<k; i++)//j<i<k。 for(j=1; j<i; j++) ans=min(ans,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]); }}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { int x,y,v; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) a[i][j]=INF; for(int i=1; i<=m; i++) { scanf("%d%d%d",&x,&y,&v); if(v<a[x][y]) a[x][y]=a[y][x]=v; } for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) d[i][j]=a[i][j]; Floyd(); if(ans!=INF) printf("%d\n",ans); else printf("It's impossible.\n"); } return 0;}
0 0
- 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+最小环)
- HDU1599 find the mincost route (floyd求最小环)
- hd 1599 find the mincost route(floyd)
- hdu 1599 find the mincost route(Floyd)
- HDU1599:find the mincost route(Floyd)
- floyd寻找最短环find the mincost route
- HDU1599 find the mincost route (Floyd)
- HDU 1599 find the mincost route (floyd)
- hdu 1599 find the mincost route (最小环与floyd算法)
- find the mincost route
- find the mincost route
- find the mincost route
- find the mincost route
- find the mincost route
- find the mincost route
- 构建Android 开发环境
- jprofile8意外关闭
- ARM CPU 简单分类
- HDU-3295-An interesting mobile game(BFS+DFS)
- php session设置说明
- find the mincost route(floyd算法)
- enable_shared_from_this
- 博客地址
- Smart Config工作原理
- 黑马程序员_Map集合
- 数据文件元格式
- <Twitter>temproxy开源memcache/redis代理
- 归并排序求逆序数
- POJ 1161 Walls(建图+Floyd)