题目大意:给出一个无向边,非常多询问,问x,y两地之间的最长路最短是多少。

思路:乍一看好像是二分啊。

的确这个题二分能够做。可是时间会慢非常多,有的题直接就T掉(NOIP2013货车运输)。

事实上这个题的模型就是最小瓶颈路模型。

解法就是把无向图变成一个最小生成树,然后两点之间的最长路就是满足题意的答案。

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 15010
#define INF 0x7f7f7f7f
using namespace std; struct Complex{
int x,y,len; bool operator <(const Complex &a)const {
return len < a.len;
}
void Read() {
scanf("%d%d%d",&x,&y,&len);
}
}edge[MAX << 2]; int points,edges,asks;
int head[MAX],total;
int next[MAX << 1],length[MAX << 1],aim[MAX << 1]; int fa[MAX]; int deep[MAX];
int father[MAX][20],min_length[MAX][20]; void Pretreatment();
int Find(int x); inline void Add(int x,int y,int len); void DFS(int x,int last);
void SparseTable(); int GetLCA(int x,int y); int main()
{
cin >> points >> edges >> asks;
Pretreatment();
for(int i = 1;i <= edges; ++i)
edge[i].Read();
sort(edge + 1,edge + edges + 1);
for(int i = 1;i <= edges; ++i) {
int fx = Find(edge[i].x);
int fy = Find(edge[i].y);
if(fx != fy) {
Add(edge[i].x,edge[i].y,edge[i].len);
Add(edge[i].y,edge[i].x,edge[i].len);
fa[fx] = fy;
}
}
DFS(1,0);
SparseTable();
for(int x,y,i = 1;i <= asks; ++i) {
scanf("%d%d",&x,&y);
printf("%d\n",GetLCA(x,y));
}
return 0;
} void Pretreatment()
{
for(int i = 1;i <= points; ++i)
fa[i] = i;
} int Find(int x)
{
if(fa[x] == x) return x;
return fa[x] = Find(fa[x]);
} inline void Add(int x,int y,int len)
{
next[++total] = head[x];
aim[total] = y;
length[total] = len;
head[x] = total;
} void DFS(int x,int last)
{
deep[x] = deep[last] + 1;
for(int i = head[x];i;i = next[i]) {
if(aim[i] == last) continue;
father[aim[i]][0] = x;
min_length[aim[i]][0] = length[i];
DFS(aim[i],x);
}
} void SparseTable()
{
for(int j = 1;j < 20; ++j)
for(int i = 1;i <= points; ++i) {
father[i][j] = father[father[i][j - 1]][j - 1];
min_length[i][j] = max(min_length[i][j - 1],min_length[father[i][j - 1]][j - 1]);
}
} int GetLCA(int x,int y)
{
int re = 0;
if(deep[x] < deep[y]) swap(x,y);
for(int i = 19;i >= 0; --i)
if(deep[father[x][i]] >= deep[y]) {
re = max(re,min_length[x][i]);
x = father[x][i];
}
if(x == y) return re;
for(int i = 19;i >= 0; --i)
if(father[x][i] != father[y][i]) {
re = max(re,min_length[x][i]);
re = max(re,min_length[y][i]);
x = father[x][i];
y = father[y][i];
}
re = max(re,min_length[x][0]);
re = max(re,min_length[y][0]);
return re;
}

BZOJ 3732 Network 最小瓶颈路的更多相关文章

  1. 最小瓶颈路 Uva 534 Frogger

    说明:关于Uva的题目,可以在vjudge上做的,不用到Uva(那个极其慢的)网站去做. 最小瓶颈路:找u到v的一条路径满足最大边权值尽量小 先求最小生成树,然后u到v的路径在树上是唯一的,答案就是这 ...

  2. UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)

    题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...

  3. UVA 11354 Bond(最小瓶颈路+倍增)

    题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...

  4. 【UVA534】Frogger 最小瓶颈路

    题目大意:给定一张 N 个点的完全图,求 1,2 号节点之间的一条最小瓶颈路. 题解:可知,最小瓶颈路一定存在于最小生成树(最小瓶颈树)中.因此,直接跑克鲁斯卡尔算法,当 1,2 号节点在同一个联通块 ...

  5. 【20181102T2】飞越行星带【智商题+最小瓶颈路】

    题面 [正解] 一眼不可做啊 --相当于求路线上穿过的点最小距离最大 最小最大--二分啊 现在相当于给一个直径,要判断这个直径是否能从左边穿到右边 我们可以在距离不超过直径的点连一条边,\(y=0\) ...

  6. UVa 11354 邦德(最小瓶颈路+LCA)

    https://vjudge.net/problem/UVA-11354 题意: 有n个城市m条道路,每条道路有一个危险系数.先在有若干个询问,要求找到一条从s到t的路,使得途径所有边的最大危险系数最 ...

  7. 【UVA10816】Travel in Desert (最小瓶颈路+最短路)

    UVA10816 Travel in Desert 题目大意 沙漠中有一些道路,每个道路有一个温度和距离,要求s,t两点间的一条路径,满足温度最大值最小,并且长度最短 输入格式 输入包含多组数据. 每 ...

  8. HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  9. P1396 营救(最小瓶颈路)

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

随机推荐

  1. Java-获取堆的大小

    package com.tj; public class getHeapInfo { public static void main(String[] args) { //获取当前堆的大小 byte ...

  2. STM32F407 SPI 个人笔记

    概述 SPI ,Serial Peripheral interface,串行外围设备接口 全双工,同步的通信总线,四根线 主要应用在 EEPROM,FLASH,实时时钟,AD转换器,还有数字信号处理器 ...

  3. CodeForces 321C Ciel the Commander

    Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  4. NYOJ 293 Sticks

    Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 George took sticks of the same length and cut them r ...

  5. 多线程下,多次操作数据库报错,There is already an open DataReader associated with this Command which must be closed first.

    原文:https://www.cnblogs.com/sdusrz/p/4433108.html 执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者 ...

  6. 九度oj 题目1035:找出直系亲属

    题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild ...

  7. Codeforces 547B. Mike and Feet[单调栈/队列]

    这道题用单调递增的单调栈维护每个数能够覆盖的最大区间即可. 对于   1 2 3 4 5 4 3 2 1 6 这组样例, 1能够覆盖的最大区间是10,2能够覆盖的最大区间是7,以此类推,我们可以使用单 ...

  8. [kubernetes] 使用 Minikube 快速搭建本地 k8s 环境 (基于 Docker 驱动模式)

    一.实验环境 操作系统:Centos 7 x86_64 Docker:1.12.6 二.部署 k8s 步骤 2.1  安装 kubectl cat <<EOF > /etc/yum. ...

  9. 解开Future的神秘面纱之任务执行

    此文承接之前的博文 解开Future的神秘面纱之取消任务 补充一些任务执行的一些细节,并从全局介绍程序的运行情况. 任务提交到执行的流程 前文我们已经了解到一些Future的实现细节,这里我们来梳理一 ...

  10. PHP提示Cannot modify header information - headers already sent by解决方法

    PHP提示Cannot modify header information - headers already sent by解决方法 因为 header();发送头之前不能有任何输出,空格也不行, ...