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 ...
随机推荐
- bootstrap基础知识
Bootstrap是Twitter推出的一款简洁.直观.强悍的前端开发框架. Bootstrap基于 HTML.CSS.JAVASCRIPT.它由Twitter的设计师Mark Otto和Jacob ...
- 转:你真的懂得JS吗?
题目1 if (!("a" in window)) { var a = 1; } alert(a); // undefined, ~~~所有全局变量都是window的属性,声明语句 ...
- BZOJ 1600: [Usaco2008 Oct]建造栅栏
1600: [Usaco2008 Oct]建造栅栏 Time Limit: 5 Sec Memory Limit: 64 MB Description 勤奋的Farmer John想要建造一个四面的 ...
- Exploring the MapBox stack: MBTiles, TileJSON, UTFGrids and Wax
转自:http://blog.thematicmapping.org/2012/11/exploring-mapbox-stack-mbtiles-tilejson.html In my last b ...
- 【组队赛三】-C cf448B
Suffix Structures Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit S ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...
- C++静态成员函数和静态成员变量的探索
静态数据成员属于类,非属于类对象,所以,定义位置就有了限制. 静态数据成员要实际地分配空间,故不能在类的声明中定义(只能声明数据成员).类声明只声明一个类的“尺寸和规格”,并不进行实际的内存分配,所以 ...
- Android:ServiceDemo
效果图: layout的main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- CentOS6.5实现rsync+inotify实时同步
参考博文: 参考1:CentOS6.5实现rsync+inotify实时同步 参考2:inotify-tools+rsync实时同步文件安装和配置 CentOS 6.3下rsync服务器的安装与配置 ...
- centos下yum安装crontab+mysql自动备份
参考博文: centos下yum安装crontab yum install vixie-cron crontabs //安装 chkconfig crond on ...