转载请注明出处: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. PROPAGATION_REQUIRED

    PROPAGATION_REQUIRED (2009-05-13 13:26:52) 转载▼   事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传 ...

  2. 第四种:GCD

    GCD 1> 概述 Grand Central Dispatch (GCD)是Apple开发的一种多核编程技术.主要用于优化应用程序以支持多核处理器以及其他对称多处理系统. GCD提供函数实现多 ...

  3. android 仿小米icon处理,加阴影和边框

    本人自己在做一个launcher,所以须要处理icon,加阴影和边框等.这仅仅是一种处理方法,其它的处理方法类似. 源码: https://github.com/com314159/LauncherI ...

  4. Android开发之TextView排版问题

    下面直接是关于解决该问题的代码(根据别人的代码进行了修正以及测试,保证可以修改字体尺寸.颜色.根据padding调整,如果需要支持其他的格式可以将对应的属性添加至Paint类型的对象中):   1 p ...

  5. 一次搞定cocos2d-x的 Android.mk 文件

    cocos2d-x 打 android 包时要修改 Android.mk 文件,但每次修改很麻烦,如果源文件很多的话就坑死了,也可以写一个脚本来自动生成这个文件,但每次还要重新生成一下,将其修改下面的 ...

  6. 《数据通信与网络》笔记--虚电路网络:帧中继和ATM

    在之前的文章中已经介绍过虚电路交换,详细请参见:http://blog.csdn.net/todd911/article/details/9069447 这边介绍下使用虚电路交换的2中WAN技术:帧中 ...

  7. 显示器中关于HS,VS,HBP,VBP参数浅析

    先来解释几个缩写的含义 HSYNC : 水平同步信号(horizontal synchronization signal) VSYNC :  垂直同步(Vertical Sync):场同步 FR : ...

  8. nginx access_log 完全关闭

    最近在配置本地nginx开发环境时,发现一个问题,当server段不指定access_log时,并且http段中也未指定任何 access_log参数时,它会默认写到logs/access.log这个 ...

  9. php __autoload使用

    官方介绍: void __autoload ( string $class ) 你可以通过定义这个函数来启用类的自动加载. 转载一篇文章: PHP autoload机制详解 (1) autoload机 ...

  10. php前端控制器设计1

    The primary role of a front controller in web-based applications is to encapsulate the typical reque ...