转载请注明出处:http://blog.csdn.net/a1dark

分析:终于弄懂了floyd的原理、以前的理解一直肤浅、所以一做到floyd应用的题、就拙计了、其实floyd的本质DP、利用前K-1个点、便可以求出当前所成的最小环、具体实现如下(含注释):

#include<stdio.h>
#include<string.h>
#define N 101
#define INF 0x7ffffff
int 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最小环详解)的更多相关文章

  1. HDU 1599 find the mincost route(floyd求最小环 无向图)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  2. hdoj 1599 find the mincost route【floyd+最小环】

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. hdu 1599 find the mincost route (最小环与floyd算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  4. hdu 1599 find the mincost route floyd求无向图最小环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu 1599 find the mincost route(无向图的最小环)

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. hdu 1599 find the mincost route 最小环

    题目链接:HDU - 1599 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须 ...

  7. HDU 1599 find the mincost route (无向图的最小环)

    题意: 给一个带权无向图,求其至少有3个点组成的环的最小权之和. 思路: (1)DFS可以做,实现了确实可以,只是TLE了.量少的时候应该还是可以水一下的.主要思路就是,深搜过程如果当前点搜到一个点访 ...

  8. HDU 1599 find the mincost route (最短路 floyd)

    题目链接 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....V ...

  9. hdu 1599 find the mincost route

    http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...

随机推荐

  1. border-radius.htc为ie6-8实现圆角

    ~~圆角是比较常用的css3属性,但是ie6-8并不支持圆角,可用border-radius.htc  html组件实现圆角, border-radius.htc内部应用vml进行了重绘 border ...

  2. JAVA Metrics 度量工具使用介绍

    转载: http://blog.csdn.net/scutshuxue/article/details/8350135 http://koven2049.iteye.com/blog/968143 h ...

  3. Windows Phone 8初学者开发—第8部分:理解编译和部署

    原文 Windows Phone 8初学者开发—第8部分:理解编译和部署 第8部分:理解编译和部署 原文地址: http://channel9.msdn.com/Series/Windows-Phon ...

  4. jQuery入门学习贴

    先引一下阮一峰大牛翻译的书<软件随想录>中的一句话: “ joel认为,软件开发,最缺的是天才.而我认为,最缺的是全才.什么“全”:全面.全局.全心全意.全力以赴. ” 最近再学习jQue ...

  5. isdigit()判断是不是数字

    string 里面的函数isdigit(),可以判断是不是数字. 或者,采用type(1)==int.

  6. BZOJ 1622: [Usaco2008 Open]Word Power 名字的能量

    题目 1622: [Usaco2008 Open]Word Power 名字的能量 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 349  Solved ...

  7. java打印日历

    打个日历,写了半天,感情水平真菜, 不过主要是不会数组,明天一定要把数组看了 package demo; import java.util.Scanner; public class Demo { / ...

  8. Python 第十二篇:HTML基础

    一:基础知识: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可 ...

  9. tabbar动画切换

    效果1: UIViewController *vc = self.viewControllers[self.selectedIndex]; CATransition *animation =[CATr ...

  10. QML在XP等显卡明显不好的情况下 可以参考

    http://doc.qt.io/qt-5/windows-requirements.html