BZOJ1599 find the mincost route 【floyd】
题目链接
题解
最小环模板?周末了养生一下【逃】
解释一下原理
\(floyd\)算法每一轮求出以\([1,k]\)为中介点的最短路
我们对于一个环,考虑环上编号最大的点,在\(k\)枚举到那个点时,\(k\)两边的点之间不经过\(k\)的最短路已经计算出来,相连接便是一个环
容易发现最小的环一定会被计算
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 105,maxm = 100005,INF = 100000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,G[maxn][maxn],d[maxn][maxn],minc;
void floyd(){
REP(i,n) REP(j,n) d[i][j] = G[i][j];
REP(k,n){
for (int i = 1; i < k; i++)
for (int j = i + 1; j < k; j++)
minc = min(minc,d[i][j] + G[j][k] + G[k][i]);
REP(i,n) REP(j,n) d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
}
}
int main(){
while (~scanf("%d%d",&n,&m)){
REP(i,n) REP(j,n) G[i][j] = INF;
int a,b,w; minc = INF;
while (m--){
a = read(); b = read(); w = read();
if (G[a][b] > w) G[a][b] = G[b][a] = w;
}
floyd();
if (minc >= INF) puts("It's impossible.");
else printf("%d\n",minc);
}
return 0;
}
BZOJ1599 find the mincost route 【floyd】的更多相关文章
- 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 ...
- AngularJS之高级Route【三】(八)
前言 我们知道默认的路由提供(Route Provider)在复杂的应用程序中是不太适合应用场景,它存在诸多限制,所以在Angular 1.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 ...
- 【Floyd】珍珠
[题目描述] 有n颗形状和大小都一致的珍珠,它们的重量都不相同.n为整数,所有的珍珠从1到n编号.你的任务是发现哪颗珍珠的重量刚好处于正中间,即在所有珍珠的重量中,该珍珠的重量列(n+1)/2位.下面 ...
- find the mincost route【无向图最小环】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在860 ...
- AngularJS之中级Route【二】(七)
前言 上一篇我们介绍了AngularJS内置的路由ngRoute,我们知道AngularJS被广泛应用于单页应用SPA(Single Page Application)中,此时路由对于我们来讲非常重要 ...
- 【floyd】HDU 1874 畅通project续
之后的题解偏重有用/总结性质,尽量理解算法本身而不是题,时间复杂度什么的也能够放放. 非常久之前做过这个题,当时使用dijkstra做的,关于几个最短路算法,分类的话能够分为下面几种. 1.单源最短路 ...
- 【Floyd】文化之旅
[NOIP2012]文化之旅 题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一 种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不 ...
- AngularJS之初级Route【一】(六)
前言 这一节我们来讲讲AngularJS中的路由以及利用AngularJS在WebAPi中进行CRUD.下面我们一起来看看. 话题 当我们需要进行路由映射时即用到$route服务,在AngularJS ...
随机推荐
- Consul初体验
Preface Today I'm gonna implement a consul in my environment to discover service of MySQL da ...
- WebDriver--定位元素的8种方式
在UI层面的自动化测试开发中,元素的定位与操作是基础,也是经常遇到的困难所在.webdriver提供了8种定位: 1. id定位:find_element_by_id("id值") ...
- Java开发工程师(Web方向) - 03.数据库开发 - 期末考试
期末考试 编程题 本编程题包含4个小题,覆盖知识点从基础的JDBC.连接池到MyBatis. 1(10分) 有一款在线教育产品“天天向上”主要实现了在手机上查看课程表的功能.该产品的后端系统有一张保存 ...
- Java学习 · 初识 IO流
IO流 1. 原理与概念 a) 流 i. 流动,流向 ii. 从一端移动到另一端 源头到目的地 iii. 抽象.动态概念,是一连 ...
- RSA算法笔记+理解
明天网络安全考试了,看了一下午,还没理解透,持续更新... 质数: 除了1和它本身以外不再有其他因素的数互质关系: 两个正整数,除了1以外,没有其他公因子RSA实现了非对称加密DES实现了对称加密** ...
- 手机端网页返回顶部js代码
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...
- Kali渗透测试工具-nslookup
1.交互模式 终端输入nslookup进入交互模式 (1)查询A地址记录(默认) set q=a A记录简单理解将域名转换成对应的IP地址 (2)查询mail exchanger set q=mx m ...
- http://www.yiibai.com/javalang/string_endswith.html
http://www.yiibai.com/javalang/string_endswith.html
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- Special Offer! Super Price 999 Bourles!
Description Polycarpus is an amateur businessman. Recently he was surprised to find out that the mar ...