HDU 1599 find the mincost route (无向图floyd最小环详解)
来源:互联网 发布:ubuntu lnmp 编辑:程序博客网 时间:2024/05/21 18:45
转载请注明出处:http://blog.csdn.net/a1dark
分析:终于弄懂了floyd的原理、以前的理解一直肤浅、所以一做到floyd应用的题、就拙计了、其实floyd的本质DP、利用前K-1个点、便可以求出当前所成的最小环、具体实现如下(含注释):
#include<stdio.h>#include<string.h>#define N 101#define INF 0x7ffffffint mpt[N][N];int dist[N][N];int m,n,minc;int min(int x,int y){ if(x<y) return x; return y;}void floyd(){ minc=INF; for(int k=1;k<=n;k++){//前K-1个点的情况递推前K个点的情况 for(int i=1;i<=k;i++) for(int j=i+1;j<=k;j++)//两个点必然是不同的 minc=min(minc,dist[i][j]+mpt[i][k]+mpt[k][j]);//K为环的最大点、无向图三点成环 for(int i=1;i<=n;i++)//floyd算法求任意两点的最短路、包含前K-1个点 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]; }}void init(){//初始化必须全部都为无穷大、因为自身不能成环 for(int i=0;i<N;i++) for(int j=0;j<N;j++){ mpt[i][j]=INF; dist[i][j]=INF; }}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ init(); int s,e,v; for(int i=1;i<=m;i++){ scanf("%d%d%d",&s,&e,&v); if(v<mpt[s][e]){ mpt[s][e]=v; mpt[e][s]=v; dist[s][e]=v; dist[e][s]=v; } } floyd(); if(minc<INF) printf("%d\n",minc); else printf("It's impossible.\n"); } return 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 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(找无向图最小环)
- HDU 1599 find the mincost route(无向图最小环)
- HDU 1599 find the mincost route(无向图最小环)
- 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(无向图最小环)
- HDU 1599 find the mincost route Floyd求最小环
- python3.x中的urllib模块
- HTTP请求流程(一)----流程简介
- 简单的html代码 加粗 加亮 字型加大 变色 href去下划线、边框
- perl学习(9) 实例:取出操作时间最长的100个记录
- Excel 输入中文报错
- HDU 1599 find the mincost route (无向图floyd最小环详解)
- 7个示例科普CPU Cache
- 堆栈
- hdu4725 The Shortest Path in Nya Graph
- nyoj-94cigarettes
- 黑马程序员------多线程(No.2)(死锁、线程间通信、生产者消费者问题)
- 最近出现问题
- shell 的字符串操作
- iOS进阶面试题----Block部分