题意

\(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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

    传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...

  4. 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 ...

  5. hdu 1599 find the mincost route(无向图的最小环)

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. 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 ...

  7. 【HDU】2191 多重背包问题

    原题目:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 [算法]多重背包(有限背包) 动态规划 [题解]http://blog.csdn.net/acdreamers/article/detail ...

  8. 【HDU】6110 路径交(2017百度之星) 线段树+RMQ-LCA+树链的交

    [题目]2017"百度之星"程序设计大赛 - 初赛(A) [题意]给定n个点的带边权树,m条编号1~m的路径,Q次询问编号区间[L,R]所有链的交集的长度.n<=500000 ...

  9. 【HDU】6148 Valley Numer 数位DP

    [算法]数位DP [题意]定义V-number为从左到看单位数字未出现先递增后递减现象的数字,求0~N中满足条件的数字个数.T<=200,lenth(n)<=100 [题解]百度之星201 ...

随机推荐

  1. CRUD查询

    简单查询: 1.最简单的查询 select*form 表名; *查所有的列select*form info 2.查询指定列 select code,name form info 3.修改结果集的列名 ...

  2. nginx配置反向代理解决前后端分离跨域问题

    摘自<AngularJS深度剖析与最佳实践>P132 nginx配置文件如下: server { listen ; server_name your.domain.name; locati ...

  3. layui常用方法

    很好用的一个ui组件,弹出,分页等 http://layer.layui.com/ 1 带叉叉的弹窗 layer.open({ type: , title: false, //不显示标题 conten ...

  4. pwd命令

    [pwd]      打印当前的工作目录             pwd==print work director 命令格式: pwd [OPTION]... 命令功能: 打印当前工作目录的全路径 命 ...

  5. 使用KRPano资源分析工具解密被加密的XML

    软件交流群:571171251(软件免费版本在群内提供) krpano技术交流群:551278936(软件免费版本在群内提供) 最新博客地址:blog.turenlong.com 限时下载地址:htt ...

  6. - >code vs 1475 m进制转十进制

    1475 m进制转十进制  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解  查看运行结果   题目描述 Description 将m进制数n转化成一个 ...

  7. The note of Vue.js

    In computed field, increment operator is not supported.

  8. sublime text 2 快捷键

    快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t 重新打开最近关闭文件 ctrl+n 新建文件 c ...

  9. springmvc session和model解析

    关于springMVC中的session,有2种使用方法,第一种是直接传递httpsession,第二种是使用@SessionAttributes("userId") 注解 这里附 ...

  10. 整数转IP地址

    将一个整数,比如1567898765转换为xxx.xxx.xxx.xxx的IP地址的形式, 以下是源代码 union IPNode{ unsigned int addr; struct { unsig ...