题解 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 & ...
随机推荐
- less : 写一个display:flex的mixin
和scss一样,less也是一个好用的css预处理语言,语法也很相近. 而我们在使用display:flex的时候,很容易苦恼于里面的设置的单词很难记(尤其是对我这种英语很差的人来说). 所以我们可以 ...
- vscode用MinGW和Clang配置C++环境
vscode是个不错的编辑器,简洁轻量,就是一开始的时候需要对环境进行配置,开个贴记录一下怎么用MingGW64+clang来配置. 下载地址 LLVM Download Page Pre-Built ...
- italic和oblique的区别
italic和oblique都是向右倾斜的文字, 但区别在于Italic是指斜体字,而Oblique是倾斜的文字(让没有斜体属性的文字倾斜), 对于没有斜体的字体应该使用Oblique属性值来实现倾斜 ...
- sql 大小写查询 字符串替换 小写xx 改为大写XX
--sql 大小写查询 select * from 表 where 字段 collate Chinese_PRC_CS_AS='xx' --替换 小写xx 改为大写XX update 表 set ...
- 单例模式—Java实现
饿汉法 顾名思义,饿汉法就是在第一次引用该类的时候就创建对象实例,而不管实际是否需要创建.代码如下: 1 2 3 4 5 6 7 public class Singleton { priv ...
- 【IJCAI2020】Split to Be Slim: An Overlooked Redundancy in Vanilla Convolution
Split to Be Slim: An Overlooked Redundancy in Vanilla Convolution, IJCAI 2020 论文地址: https://arxiv.or ...
- JavaFX布局神器-SceneBuilder
JavaFX允许开发使用FXML来设计和布局界面,跟Qt和Android的布局有点类似,JavaFX用SceneBuilder来设计和布局界面. SceneBuilder最新的下载地址:https:/ ...
- Zabbix3.2安装
一.环境 OS: CentOS7.0.1406 Zabbix版本: Zabbix-3.2 下载地址: http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/z ...
- 使用MacOS直播
参考链接:https://www.jianshu.com/p/94f42a793a7e 参考链接:https://blog.dreamtobe.cn/live_guideline/ 所需软件 密码: ...
- postman~界面介绍
本文摘抄自https://www.jianshu.com/p/b8b02afa74b1 官方文档:https://learning.getpostman.com/docs/postman/launch ...