【HDU】1599 find the mincost route
题意
\(n(1 \le n \le 100)\)个点\(m(1 \le m \le 1000)\)条加权边的无负环无向图,求一个最小环。
分析
加入有一个环,其编号最大的点为\(L\),那么这个环可以看为\(L\)与其相邻的两个点\(A\)和\(B\)与\(A\)到\(B\)的最短路上的点(编号均小于\(L\)的最短路)。
考虑floyd算法,由于该算法每次都是求出了\(1\)到\(k-1\)做为中间点的最短路然后来求已\(k\)为中间点的最短路,则我们可以将其拓展到求最小环。
题解
如分析
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=105, oo=0x3f3f3f3f;
int n, m, a[N][N], d[N][N];
int main() {
while(~scanf("%d%d", &n, &m)) {
int ans=oo;
memset(d, 0x3f, sizeof d);
memset(a, 0x3f, sizeof a);
while(m--) {
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
if(a[x][y]>w) {
a[x][y]=a[y][x]=d[x][y]=d[y][x]=w;
}
}
for(int k=1; k<=n; ++k) {
for(int i=1; i<k; ++i) if(a[i][k]!=oo) {
for(int j=1; j<i; ++j) if(a[j][k]!=oo && d[i][j]!=oo) {
ans=min(ans, a[i][k]+a[j][k]+d[i][j]);
}
}
for(int i=1; i<=n; ++i) if(d[i][k]!=oo) {
for(int j=1; j<=n; ++j) if(d[k][j]!=oo) {
d[i][j]=min(d[i][j], d[i][k]+d[k][j]);
}
}
}
if(ans==oo) {
puts("It's impossible.");
}
else {
printf("%d\n", ans);
}
}
return 0;
}
【HDU】1599 find the mincost route的更多相关文章
- 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算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...
- 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】
传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...
- 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(无向图的最小环)
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求无向图最小环
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 【HDU】2191 多重背包问题
原题目:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 [算法]多重背包(有限背包) 动态规划 [题解]http://blog.csdn.net/acdreamers/article/detail ...
- 【HDU】6110 路径交(2017百度之星) 线段树+RMQ-LCA+树链的交
[题目]2017"百度之星"程序设计大赛 - 初赛(A) [题意]给定n个点的带边权树,m条编号1~m的路径,Q次询问编号区间[L,R]所有链的交集的长度.n<=500000 ...
- 【HDU】6148 Valley Numer 数位DP
[算法]数位DP [题意]定义V-number为从左到看单位数字未出现先递增后递减现象的数字,求0~N中满足条件的数字个数.T<=200,lenth(n)<=100 [题解]百度之星201 ...
随机推荐
- apache极简配置虚拟机
在apache的httpd.conf文件最后添加: Listen 81<VirtualHost *:81> DocumentRoot E:/CM/wx_shop/ecshop</Vi ...
- 基于libvlc和wxWidgets的简单播放器代码阅读
源代码来自 http://git.videolan.org/?p=vlc.git;a=blob_plain;f=doc/libvlc/wx_player.cpp // g++ wx_player.cp ...
- Win10搭建Linux开发环境之网络连接设定
一直想在家自己搭建一个LINUX服务器,好在上面安装个ORACLE数据库玩玩. 上次用了Ubuntu,结果ORACLE没装成功,现在换个思路,采用CentOS 7作为Linux服务器, 之后再进行构建 ...
- Bootstrap表单验证插件bootstrapValidator使用方法整理
插件介绍 先上一个图: 下载地址:https://github.com/nghuuphuoc/bootstrapvalidator 使用方法:http://www.cnblogs.com/huangc ...
- 函数floor(n)
Matlab函数floor(n): 仅取整数部分,剔除小数部分 例如: floor(9.2)=9; 仅此
- 使用KRPano资源分析工具分析动态网站资源
软件交流群:571171251(软件免费版本在群内提供) krpano技术交流群:551278936(软件免费版本在群内提供) 最新博客地址:blog.turenlong.com 限时下载地址:htt ...
- java正则表达式获得html字符串中<img src>的src中的url地址
/** * 得到网页中图片的地址 */ public static Set<String> getImgStr(String htmlStr) { Set<String> pi ...
- [原创]Windows Server 2003 物理机转换为VMware虚拟机出现VSS错误的处理
一台Windows Server 2003 物理机需要转换为VMware虚拟机,工具为Vmware vCenter Converter Standalone 6.0,转换开始就出现错误“FAILED: ...
- freemarker IllegalAccessError 错误
java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashModel.& ...
- Linux Yum 安装JDK
1.查看CentOS自带JDK是否已安装. 输入:yum list installed |grep java. 2.若有自带安装的JDK,如何卸载CentOS系统自带Java环境? 卸载JDK相关文件 ...