题目大意:另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的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. SQL 给某字段添加汉字却显示??

    错误展示: 解决方案: 1.在要修改的数据库上单击鼠标右键,并选择“属性”.   2.在弹出的数据库属性窗口中点击“选择页”中的“选项”.   3.将排序规则由默认的SQL_Latin1_Genera ...

  2. Git的忽略文件

    *.iml.gradle.idea/local.properties/.idea/workspace.xml/.idea/libraries.DS_Store/build/captures.exter ...

  3. 曹工说Spring Boot源码(30)-- ConfigurationClassPostProcessor 实在太硬核了,为了了解它,我可能debug了快一天

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  4. jmeter正则表达式,萌新入门篇

    @@@@@@@@@@@@ 透过现象看本质 jmeter中正则表达式对我们来说,就是一个工具,他可以帮助我们做的事就是从一堆数据中截取出我们想要的字段,比如从setcookie:DERF12456DAS ...

  5. JAVA实现BP神经网络算法

    工作中需要预测一个过程的时间,就想到了使用BP神经网络来进行预测. 简介 BP神经网络(Back Propagation Neural Network)是一种基于BP算法的人工神经网络,其使用BP算法 ...

  6. matplotlib 显示中文问题

    import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签plt.rcParams[' ...

  7. PHP utf8_decode() 函数

    定义和用法 utf8_decode() 函数把 UTF-8 字符串解码为 ISO-8859-1.高佣联盟 www.cgewang.com 该函数把通过 utf8_encode() 函数编码的 ISO- ...

  8. PHP get_browser() 函数

    实例 查阅用户的 browscap.ini 文件,并返回用户浏览器的性能: <?phpecho $_SERVER['HTTP_USER_AGENT'];$browser = get_browse ...

  9. luogu P4798 [CEOI2015 Day1]卡尔文球锦标赛 dp 数位dp

    LINK:卡尔文球锦标赛 可以先思考一下合法的序列长什么样子. 可以发现后面的选手可以使用前面出现的编号也可以直接自己新建一个队. 其实有在任意时刻i 序列的mex>max.即要其前缀子序列中1 ...

  10. 6.28 NOI模拟赛 好题 状压dp 随机化

    算是一道比较新颖的题目 尽管好像是两年前的省选模拟赛题目.. 对于20%的分数 可以进行爆搜,对于另外20%的数据 因为k很小所以考虑上状压dp. 观察最后答案是一个连通块 从而可以发现这个连通块必然 ...