HDU 1599 find the mincost route (无向图floyd最小环详解)
转载请注明出处: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最小环详解)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- hdu 1599 find the mincost route(无向图的最小环)
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1599 find the mincost route 最小环
题目链接:HDU - 1599 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须 ...
- HDU 1599 find the mincost route (无向图的最小环)
题意: 给一个带权无向图,求其至少有3个点组成的环的最小权之和. 思路: (1)DFS可以做,实现了确实可以,只是TLE了.量少的时候应该还是可以水一下的.主要思路就是,深搜过程如果当前点搜到一个点访 ...
- HDU 1599 find the mincost route (最短路 floyd)
题目链接 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....V ...
- hdu 1599 find the mincost route
http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...
随机推荐
- poj 2021 Relative Relatives(暴力)
题目链接:http://poj.org/problem?id=2021 思路分析:由于数据较小,采用O(N^2)的暴力算法,算出所有后代的年龄,再排序输出. 代码分析: #include <io ...
- C 语言统计关键字出现次数
#include <stdio.h> #include <ctype.h> #include <string.h> #define NKEYS (sizeof ke ...
- c# datagridviewcomboboxcell值无效的解决办法
一直认为是数据库存储的数据和datagridviewcomboboxcell对不上导致,今天碰到两者对应上了,预览的时候还是提示错误, 查看了下网上其他大神的解决方法,是数据库字段类型有误,查看了下, ...
- 【JavaScript】==与===对比
1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直接进 ...
- 循环次数( M - 暴力求解、打表)
循环次数 Description 我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分.例如, 如果代码中出现 for(i=1;i ...
- 我的Python成长之路---第三天---Python基础(12)---2016年1月16日(雾霾)
四.函数 日常生活中,要完成一件复杂的功能,我们总是习惯把“大功能”分解为多个“小功能”以实现.在编程的世界里,“功能”可称呼为“函数”,因此“函数”其实就是一段实现了某种功能的代码,并且可以供其它代 ...
- (Problem 3)Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...
- sqlite可视化工具以及django-South
在linux系统下想要可视化sqlite的办法 一:安装sqlitebrowser: sudo apt-get install sqlitebrowser 二:在终端提示符后输入sqlitebrows ...
- 中科燕园GIS外包---地铁GIS项目
(1)地铁保护及project地质管理 • 地铁保护 地铁交通既有运量大,速度快的特点,又有差别于其它交通方式的在地下执行的空间特殊性,因此地铁的保护显得尤为重要. 首先必须编制完整的 ...
- sublime2 c++的一些使用配置
1 下载安装好tdw gcc后,配置好环境变量后,配置sublime2. tools->build system-> new build system... 里面输入: { "c ...