【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 ...
随机推荐
- 本地数据Store。Cookie,Session,Cache的理解。Timer类主要用于定时性、周期性任务 的触发。刷新Store,Panel
本地数据Store var monthStore = Ext.create('Ext.data.Store', { storeId : 'monthStore', autoLoad : false, ...
- JavaScript生成新标签的三个方法(摘抄自留)
<div id="d1"></div> <script> //HTML function a(){ document.getElementByI ...
- FWT与High dick(划掉改成Dimensional) Fourier Transform
我们大家都知道xor卷积有个很好的做法:FWT.FWT的变换形式是很好看的 // 说明一下Vector可以向量化运算,也可以当做数组来slice与concat Vector tf(A,2^n){ Ve ...
- 页面无刷新Upload File
页面无刷新Upload File. 利用jquery.form.js的ajaxForm提交文件. 具体参考以下代码: 前台html <%@ Page Language="C#" ...
- 《转载》使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误
今天在更新项目后进行编译时,出现如下错误一堆: 编译错误 Google之,在stackoverflow上看到如下的解决方法: I came here with the same problem. Ev ...
- Python全栈开发【模块】
Python全栈开发[模块] 本节内容: 模块介绍 time random os sys json & picle shelve XML hashlib ConfigParser loggin ...
- awk
cat map-matcher.log | awk -F '[' '{print $1}' | awk -F '-' '{print $2}' >result.txt cat 2.txt | a ...
- Macbook 的 print screen 是什么
- gradle
http://examples.javacodegeeks.com/core-java/gradle/gradle-hello-world-tutorial/
- WPF制作的VS黑色风格的Listbox
最近写的一个玩具,WPF写出来的东西还是挺好看的 style.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/ ...