hdu 1599 find the mincost route(floyd求无向图的最小环)
来源:互联网 发布:刷6次免年费淘宝算吗 编辑:程序博客网 时间:2024/05/11 14:35
题目:
链接:点击打开链接
题意:
思路:
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define INF 100000000const int N = 110;int n,m;int map[N][N],dis[N][N];int result;void floyd(){ for(int k=1; k<=n; k++) { for(int i=1; i<=k; i++) { for(int j=i+1; j<=k; j++) result = min(result,map[i][k] + map[k][j] + dis[i][j]); } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(dis[i][j] > dis[i][k] + dis[k][j]) dis[i][j] = dis[i][k] + dis[k][j]; } } }}int main(){ //freopen("input.txt","r",stdin); int a,b,c; while(scanf("%d%d",&n,&m) != EOF) { for(int i=0; i<=n; i++) { for(int j=0; j<=n; j++) { map[i][j] = dis[i][j] = INF; } } for(int i=1; i<=m; i++) { scanf("%d%d%d",&a,&b,&c); if(map[a][b] > c) map[a][b] = map[b][a] = dis[a][b] = dis[b][a] = c; } result = INF; floyd(); if(result == INF) printf("It's impossible.\n"); else printf("%d\n",result); } return 0;}
-------------------------------------------------------------------------------------
收获:
->最小环问题的解法:(无向图,有向图要考虑两点就能构成环的情况)
用Floyd的改进写法可以解决最小环问题,时间复杂度是O(n^3),代码如下:
for(int k=1; k<=n; k++) { for(int i=1; i<=k; i++)//新增部分 { for(int j=i+1; j<=k; j++) result = min(result,map[i][k] + map[k][j] + dis[i][j]); }//****************************** for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(dis[i][j] > dis[i][k] + dis[k][j]) dis[i][j] = dis[i][k] + dis[k][j]; } } }
-------------------------------------------------------------------------------------
战斗,从不退缩;奋斗,永不停歇~~~~~~~~~~~~~~~~~~~~~~~
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 1599find 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 - 1599 find the mincost route(Floyd求最小环)
- hdu 1599 find the mincost route【floyd求最小环】
- 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 无向图最小环
- spring常用注解
- 判断是否点击指定按钮
- 虚拟桌面存储考虑
- 数据结构之(二叉)堆
- 基础知识—Swift学习笔记(四)
- hdu 1599 find the mincost route(floyd求无向图的最小环)
- Oracle认证体系全攻略
- Netty入门实例及分析
- Python的数据库API操作
- Mahout学习之Mahout简介、安装、配置、入门程序测试
- Eclipse 版本
- css margin的相关属性,问题及应用
- Linux下那些奇葩的命令
- 缺少servlet-api.jar包