HDU 1599 find the mincost route (无向图floyd最小环详解)
2013-09-13 20:17
465 查看
转载请注明出处:http://blog.csdn.net/a1dark
分析:终于弄懂了floyd的原理、以前的理解一直肤浅、所以一做到floyd应用的题、就拙计了、其实floyd的本质DP、利用前K-1个点、便可以求出当前所成的最小环、具体实现如下(含注释):
分析:终于弄懂了floyd的原理、以前的理解一直肤浅、所以一做到floyd应用的题、就拙计了、其实floyd的本质DP、利用前K-1个点、便可以求出当前所成的最小环、具体实现如下(含注释):
#include<stdio.h> #include<string.h> #define N 101 #define INF 0x7ffffff int mpt ; int dist ; 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])
- HDU1599 find the mincost route(floyd求最小环)
- find the mincost route HDU - 1599(带权最小环)(Floyd)
- hdu 1599 find the mincost route(无向图最小环,floyd)
- 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 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 无向图最小环
- HDU1599 find the mincost route 无向图最小环
- HDU 1599 find the mincost route(Floyd无向图最小环)
- HDU 1599 find the mincost route【floyd 最小环】
- HDU 1599 find the mincost route(floyd求无向图)
- HDU Problem 1599 find the mincost route 【Floyd求最小环】
- HDU 1599 find the mincost route (Floyd 最小环)
- HDU 1599 find the mincost route(无向图最小环)
- hdu 1599 find the mincost route(flyod求最小环)