题解 UVA10457
题目大意:另s = 路径上的最大边权减最小边权,求u到v上的一条路径,使其s最小,输出这个s。
很容易想到枚举最小边然后跑最小瓶颈路。
so,如何跑最小瓶颈路?
利用Kruskal,因为树上两点路径唯一,而且我们是从小到大枚举边,所以如果一条边加入后u v联通,那么它一定是u到v路径上的最长边。
附上丑陋的代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct EDGE{
int u, v, w;
}edge[2000];
int fa[300], n, m;
bool cmp(const EDGE& x, const EDGE& y)
{
return x.w < y.w;
}
int find_root(int x)
{
return x == fa[x] ? x : fa[x] = find_root(fa[x]);
}
bool same(int x, int y)
{
x = find_root(x),
y = find_root(y);
return x == y;
}
bool merge(int x, int y)
{
x = find_root(x),
y = find_root(y);
fa[x] = y;
return x != y;
}
int kru(int u, int v)
{
int ans = 0x3fffffff;
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
fa[j] = j;
for (int j = i; j <= m; ++j)
if (merge(edge[j].u, edge[j].v) && same(u, v))
ans = min(ans, edge[j].w - edge[i].w);
}
return ans;
}
int main()
{
int u, v, w, ans, k;
while (~scanf("%d%d", &n, &m))
{
for (int i = 1; i <= m; ++i)
{
scanf("%d%d%d", &u, &v, &w);
edge[i] = {u, v, w};
}
sort(edge + 1, edge + 1 + m, cmp);
scanf("%d%d", &u, &v);
ans = u + v;
scanf("%d", &k);
for (int i = 1; i <= k; ++i)
{
scanf("%d%d", &u, &v);
printf("%d\n", ans + kru(u, v));
}
}
return 0;
}
题解 UVA10457的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- javascript : 复杂数据结构拷贝实验
数组深拷贝看起来很简单. array.concat()就行了. 但是,如果数组里有对象呢? 实际上,你以为你拷贝了对象,但实际上你只拷贝了对象的引用(指针)! 我们可以做个试验. // test le ...
- CSS3伪元素 ::first-letter ::first-line ::selection
首先,关于伪元素的语法: 有的时候单冒号也能用,但最好写双冒号. 伪类:匹配的是元素(不同状态或结构的). 伪元素:匹配的是元素中的一部分内容(首字符,首行文本). ::first-letter 匹配 ...
- 最全JavaScript基础总结
JavaScript介绍 什么是JavaScript? Javascript是一门面向对象的,跨平台的脚本语言. JavaScript有什么特点? 解释性脚本语言 运行在浏览器(浏览器内核带有js解释 ...
- FaaS 给前端带来了什么?
一.Serverless 与 FaaS Serverless 是一种云计算理念,即无服务器计算(Serverless Computing): Serverless suggests that the ...
- html头文件设置常用之<meta>设置
也许很多开发人员并没有重视meta标签,我就是其中一个,但是meta标签的功能很强大,下面就来说说meta标签! <meta http-equiv="pragma" cont ...
- SpringBoot+Vue项目上手
博客 https://gitee.com/RoadsideParty/White-Jotter-Vue?_from=gitee_search UI框架 https://at-ui.github.io/ ...
- Javascript 组成:ECMAscript、Dom、Bom
一.核心(ECMAScript) ECMAScript 定义的只是这门语言的基础,而在此基础之上可以构建更完善的脚本语言. 二.浏览器对象模型(BOM)——对应window对象 window:窗口 w ...
- html层重叠 相同尺寸透明flash重叠的解决办法
<EMBED style="z-index:1; position:absolute; top:110px;" src="http://www.jintaisd.c ...
- bootstrap-treeview 研究一下
一直以来都是拿来主义,现在正好有空,也正好用到,准备好好研究下bootstrap-treeview. 实现目标:可搜索,可复选选中的权限控制菜单项. 研究失败 转 jstree
- Bootstrap++:bootstrap-select 使用
效果图: HTML: <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.or ...