【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 ...
随机推荐
- 微信公众帐号开发-消息创建时间long型与标准时间的互相转换
/** * */ package com.hd.admin.wxmeet.utils; /** * @author jymcpp * */ import java.text.DateFor ...
- XCode8向ITunes提交版本,不显示或提示无效的版本
主要是iOS10,对隐私权限有了新的要求.在info.plist里加入如下代码. <key>NSCameraUsageDescription</key> <string& ...
- Ubuntu14.04源
Ubuntu14.04源: 来源: http://wiki.ubuntu.org.cn/Qref/Source (包含15.04.14.10.14.04.12.04.10.04的源) Ub ...
- Linux下的压缩和解压缩命令——bzip2/bunzip2
bzip2命令 bzip2命令用于创建和管理(包括解压缩)".bz2"格式的压缩包. 我们遇见Linux压缩打包方法有很多种,以下讲解了Linux压缩打包方法中的Linux bzi ...
- 修改Glassfish默认密码,并允许远程登录
修改默认密码 1.打开命令提示窗口,进入 glassfish/bin 目录: 2.输入命令 asadmin 进入管理模式: 接着输入: change-admin-password 注意默认密码为空,要 ...
- UFLDL 教程三总结与答案
主成分分析(PCA)是一种能够极大提升无监督特征学习速度的数据降维算法.更重要的是,理解PCA算法,对实现白化算法有很大的帮助,很多算法都先用白化算法作预处理步骤.这里以处理自然图像为例作解释. 1. ...
- 使用JSOM检查文件或文件夹是否存在
How to Check with SharePoint JSOM if File or Folder Exists Here's a code snippet showing how to use ...
- 不常用的SQL语句记录
只知道字段名,查询哪些表有该字段:假如字段名为Index select sysobjects.name as tablename,syscolumns.name as columnname from ...
- Tomcat绑定IPV4端口
使用下面的方法可以绑定到IPV4, 在<tomcat>/bin目录下新建setenv.sh,如果没有话,然后添加如下内容: JAVA_OPTS="$JAVA_OPTS -Djav ...
- python之线程进程协成
线程与进程 什么是线程 线程是进程一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位,线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源,但是它可与同属一个线程的 ...