两点之间边权最大值的最小值一定在图的最小生成树中取到。

求出最小生成树,进行倍增即可。

Code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 3;
const int maxm = 100000 + 3;
const int inf = 10000000+3;
const int logn = 30;
int st[maxm], ed[maxm], cost[maxm];
int head[maxm],to[maxm<<1], nex[maxm<<1], val[maxm<<1], cnt;
int F[maxn][logn], minv[maxn][logn], dep[maxn];
int n,m;
int cmp(int i,int j)
{
return cost[i] > cost[j];
}
struct Make_Tree
{
int A[maxm],p[maxn];
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
inline void add_edge(int u,int v,int c)
{
nex[++cnt] = head[u], head[u] = cnt, to[cnt] = v, val[cnt] = c;
}
inline void solve()
{
for(int i = 1;i <= m;++i)A[i] = i;
for(int i = 1;i <= n;++i)p[i] = i;
sort(A+1,A+1+m,cmp);
for(int i = 1;i <= m;++i)
{
int cur = A[i];
int a = st[cur], b = ed[cur];
int x = find(a);
int y = find(b);
if(x == y)continue;
add_edge(a,b,cost[cur]);
add_edge(b,a,cost[cur]);
p[x] = y;
}
}
}T;
void dfs(int u,int fa,int c,int deep)
{
F[u][0] = fa, minv[u][0] = c, dep[u] = deep;
for(int v = head[u]; v ;v = nex[v])
if(to[v] != fa){
dfs(to[v],u,val[v],deep+1);
}
}
inline int solve(int a,int b)
{
if(dep[a] > dep[b])swap(a,b);
int ans = inf;
if(dep[b] != dep[a])
{
for(int i =logn-1;i>=0;--i)
if(dep[a] <= dep[F[b][i]])
{
ans = min(ans,minv[b][i]);
b = F[b][i];
}
}
if(a == b)return ans;
for(int i = logn-1;i>=0;--i)
if(F[a][i] != F[b][i])
{
ans = min(ans,min(minv[a][i],minv[b][i]));
a = F[a][i], b = F[b][i];
}
ans = min(ans,min(minv[a][0],minv[b][0]));
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
scanf("%d%d%d",&st[i],&ed[i],&cost[i]);
T.solve();
for(int i = 1;i <= n;++i)
if(!dep[i])dfs(i,0,0,1);
for(int i = 1;i < logn-1;++i)
for(int j = 1;j <= n;++j)
{
minv[j][i] = min(minv[j][i-1], minv[F[j][i-1]][i-1]);
F[j][i] = F[F[j][i-1]][i-1];
}
int asks;
scanf("%d",&asks);
for(int i = 1;i <= asks;++i)
{
int a,b;
scanf("%d%d",&a,&b);
if(T.find(a) != T.find(b) || a>n|| b>n)printf("-1\n");
else printf("%d\n",solve(a,b));
}
return 0;
}

  

洛谷 P1967 货车运输 LCA + 最小生成树的更多相关文章

  1. 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增

    倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...

  2. 洛谷 P1967 货车运输

    洛谷 P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在 ...

  3. 【杂题总汇】NOIP2013(洛谷P1967) 货车运输

    [洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...

  4. 洛谷P1967货车运输——倍增LCA

    题目:https://www.luogu.org/problemnew/show/P1967 就是倍增LCA的裸题,注意一些细节即可. 代码如下: #include<iostream> # ...

  5. 洛谷 P1967 货车运输 Label: 倍增LCA && 最小瓶颈路

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...

  6. [洛谷 P1967] 货车运输 (最大生成树 lca)

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...

  7. 洛谷 P1967 货车运输(克鲁斯卡尔重构树)

    题目描述 AAA国有nn n座城市,编号从 11 1到n nn,城市之间有 mmm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qqq 辆货车在运输货物, 司机们想知道每辆车在不超过车 ...

  8. 洛谷P1967 货车运输

    题目描述 \(A\)国有\(n\)座城市,编号从\(1\)到\(n\),城市之间有\(m\)条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有\(q\)辆货车在运输货物, 司机们想知道每辆车在 ...

  9. [NOIP2013] 提高组 洛谷P1967 货车运输

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...

随机推荐

  1. mac pro 安装 composer 失败

    http://getcomposer.org/doc/00-intro.md#using-composer $ brew install josegonzalez/php/composer 出现错误: ...

  2. 脚本_使用expect自动交互远程主机安装软件

    #!bin/bash#功能:使用expect工具自动交互密码,远程到其它主机,安装httpd软件#作者:liusingbon#删除~/.ssh/known-hosts后,ssh远程任何主机,系统都会询 ...

  3. 第一章 关于python

    Python简介 Python是什么?   python的创始人为吉多·范罗苏姆(Guido van Rossum).  “Python is a great object-oriented, int ...

  4. java简单实现MD5加密

    1.话不多说,直接上代码-----传入字符串,返回加密码 import java.security.MessageDigest; import java.text.NumberFormat; publ ...

  5. BZOJ 3527: [Zjoi2014]力 FFT_卷积

    Code: #include <cmath> #include <cctype> #include <cstdio> #include <cstring> ...

  6. 12、Camel: Content-Aware and Meta-path Augmented Metric Learning for Author Identification----作者识别

    摘自:https://blog.csdn.net/me_yundou/article/details/80459341 具体看上面链接 一.摘要: 这篇文章主要介绍的是作者识别(author iden ...

  7. PHP下的异步尝试二:初识协程

    PHP下的异步尝试系列 如果你还不太了解PHP下的生成器,你可以根据下面目录翻阅 PHP下的异步尝试一:初识生成器 PHP下的异步尝试二:初识协程 PHP下的异步尝试三:协程的PHP版thunkify ...

  8. mysql存储小数

    线下不知道什么版本的古董了,经本人亲测,varchar类型的数据,可以直接执行mysql的sum函数. ________________________________________________ ...

  9. Redis介绍、安装部署、操作

    学习连接:http://www.runoob.com/redis/redis-tutorial.html 一.Redis介绍 Redis是NoSql的一种. NoSql,全名:Not Only Sql ...

  10. 阿里云server部署架构

    近期要上马一个项目,客户要求所有部署到阿里云的server,做了一个阿里云的部署方案. 上图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc21hbGx ...