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

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

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. VB入门在线视频教程大全学习

    课程目录 9分钟47秒 课时1第一课:怎么编写程序 14分钟1秒 课时1第十七课第1节:文件读写的几种方式 14分钟14秒 课时2第二课:什么是变量和变量类型 19分钟24秒 课时3第三课:变量的声明 ...

  2. div基本组成要素

    title下面先清除固有格式 style{ *{ margin:0 auto padding:0 foant family } } div{ width height border backgroun ...

  3. Codeforces Round #468 (Div. 2 )D. Peculiar apple-tree_BFS

    题目简单,不多解释. Code: #include<cstdio> #include<queue> using namespace std; const int maxn = ...

  4. oralce存储过程实现不同用户之间的表数据复制

    create or replace procedure prc_test123 is temp_columns ); sqltemp ); cursor cur is select table_nam ...

  5. 密信(Mesince)首创全自动邮件加密,颠覆传统邮件加密软件

    电子邮件泄密已经成为一个全球性的日益严峻的安全问题,解决这个问题的唯一有效办法就是电子邮件内容先加密后发送.然而,使用基于S/MIME标准的传统邮件加密软件进行邮件加密,需要用户具备一定的技术基础.用 ...

  6. POJ 3370 Halloween treats( 鸽巢原理简单题 )

    链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...

  7. 使用yum方式安装mysql5.6

    1.新开的云服务器,需要检测系统是否自带安装mysql # yum list installed | grep mysql 2.如果发现有系统自带mysql,果断这么干 # yum -y remove ...

  8. Python 绘图与可视化 matplotlib(上)

    参考链接:https://www.cnblogs.com/dudududu/p/9149762.html 更详细的:https://www.cnblogs.com/zhizhan/p/5615947. ...

  9. 【codeforces 743E】Vladik and cards

    [题目链接]:http://codeforces.com/problemset/problem/743/E [题意] 给你n个数字; 这些数字都是1到8范围内的整数; 然后让你从中选出一个最长的子列; ...

  10. Nutch2 WebPage 字段解释

    Nutch2 WebPage 字段解释 Nutch2.2.1 id