转载请注明出处: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. Java学习之自定义异常

    1 package com.gh; import java.util.Scanner; /** * 自定义异常 * @author ganhang * */ public class Exceptio ...

  2. hdoj Pipe&&南阳oj管道问题&&poj1039(计算几何问题...枚举)

    Pipe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. 奔小康赚大钱(km)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. android -- 蓝牙 bluetooth (四)OPP文件传输

    在前面android -- 蓝牙 bluetooth (一) 入门文章结尾中提到了会按四个方面来写这系列的文章,前面已写了蓝牙打开和蓝牙搜索,这次一起来看下蓝牙文件分享的流程,也就是蓝牙应用opp目录 ...

  5. Python中NumPy基础使用

    Python发展至今,已经有越来越多的人使用python进行科学技术,NumPY是python中的一款高性能科学计算和数据分析的基础包. ndarray ndarray(以下简称数组)是numpy的数 ...

  6. android 4.2 root

    前一段因工作需要,对android4.2 进行root.但是在下载了 点击打开链接,下载了Superuser.apk,把对应的apk拷贝到system/app,su拷贝到/system/bin 与/s ...

  7. Android的WiFi开启与关闭

    注意:要首先注册开启和关闭WiFi的权限, <?xml version="1.0" encoding="utf-8"?> <manifest ...

  8. c语言详解  蔡勒(Zeller)公式计算某一天是星期几  极其方便

    —— 蔡勒(Zeller)公式 ,小于等于14,即在蔡勒公式中,某年的1.2月要看作上一年的13.14月来计算,比如2003年1月1日要看作2002年的13月1日来计算):d:日:[ ]代表取整,即只 ...

  9. POJ 3528 求三维凸包表面积

    也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include < ...

  10. POJ1054 枚举【STL__binary_search()_的应用】

    ①使用binary_search前要先保证有序 ②binary_search函数仅返回true或false ③binary_search(first element, laste lment + 1, ...