http://www.lydsy.com/JudgeOnline/problem.php?id=3732

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1880  Solved: 894
[Submit][Status][Discuss]

Description

给你N个点的无向图 (1 <= N <= 15,000),记为:1…N。 
图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 < = d_j < = 1,000,000,000).

现在有 K个询问 (1 < = K < = 20,000)。 
每个询问的格式是:A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?

Input

第一行: N, M, K。 
第2..M+1行: 三个正整数:X, Y, and D (1 <= X <=N; 1 <= Y <= N). 表示X与Y之间有一条长度为D的边。 
第M+2..M+K+1行: 每行两个整数A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?

Output

对每个询问,输出最长的边最小值是多少。

Sample Input

6 6 8
1 2 5
2 3 4
3 4 3
1 4 8
2 5 7
4 6 2
1 2
1 3
1 4
2 3
2 4
5 1
6 2
6 1

Sample Output

5
5
5
4
4
7
4
5

HINT

1 <= N <= 15,000

1 <= M <= 30,000

1 <= d_j <= 1,000,000,000

1 <= K <= 15,000

Source

 最小生成树+倍增
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int M(+);
int n,m,k; #define LL long long
int head[N],sumedge;
struct E
{
int u,v,w;
}e[M];
bool cmp(E a,E b)
{
return a.w<b.w;
}
struct Edge
{
int v,next;
LL dis;
Edge(int v=,int next=,LL dis=):
v(v),next(next),dis(dis){}
}edge[];
inline void ins(int u,int v,LL w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} int fa[N],cnt;
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Kruskar()
{
sort(e+,e+m+,cmp);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
int x=e[i].u,y=e[i].v;
int fx=find(x),fy=find(y);
if(fa[fx]==fy) continue;
fa[fx]=fy;
ins(x,y,(LL)e[i].w);
ins(y,x,(LL)e[i].w);
if(++cnt==n-) return ;
}
} LL maxx[N];
int dad[N],deep[N];
void DFS(int x)
{
deep[x]=deep[dad[x]]+;
for(int i=head[x];i;i=edge[i].next)
{
int v=edge[i].v;
if(dad[x]==v) continue;
maxx[v]=edge[i].dis;
dad[v]=x; DFS(v);
}
}
LL LCA(int x,int y)
{
LL ret=-;
if(deep[x]>deep[y]) swap(x,y);
for(;deep[y]>deep[x];y=dad[y]) ret=max(ret,maxx[y]);
for(;x!=y;x=dad[x],y=dad[y])
ret=max(ret,max(maxx[x],maxx[y]));
return ret;
} int main()
{
scanf("%d%d%d",&n,&m,&k);
memset(maxx,-,sizeof(maxx));
for(int u,v,w,i=;i<=m;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
Kruskar();
DFS();
for(int u,v;k--;)
{
scanf("%d%d",&u,&v);
printf("%lld\n",LCA(u,v));
}
return ;
}

BZOJ——T 3732: Network的更多相关文章

  1. 【BZOJ】3732: Network【Kruskal重构树】

    3732: Network Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2812  Solved: 1363[Submit][Status][Dis ...

  2. BZOJ 3732: Network 最小生成树 倍增

    3732: Network 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 &l ...

  3. 3732: Network

    3732: Network Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 395  Solved: 179[Submit][Status] Descr ...

  4. BZOJ 3732 Network

    2016.1.28 纪念我BZOJ第一题 Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= ...

  5. bzoj 3732 Network(最短路+倍增 | LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3732 [题意] 给定一个无向图,处理若干询问:uv路径上最长的边最小是多少? [思路一 ...

  6. bzoj 3732: Network 树上两点边权最值

    http://www.lydsy.com/JudgeOnline/problem.php?id=3732 首先想到,要使得最长边最短,应该尽量走最短的边,在MST上. 然后像LCA那样倍增娶个最大值 ...

  7. BZOJ 3732 Network —— 最小生成树 + 倍增LCA

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 <= N <= 15, ...

  8. Kruskal重构树+LCA || BZOJ 3732: Network

    题面:https://www.lydsy.com/JudgeOnline/problem.php?id=3732 题解:Kruskal重构树板子 代码: #include<cstdio> ...

  9. [bzoj 3732] Network (Kruskal重构树)

    kruskal重构树 Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1-N. 图中有M条边 (1 <= M <= 30,000) ,第 ...

随机推荐

  1. Pixhawk---烧写FMU/IO bootloader

    Pixhawk-FMU/IO烧写Bootloader 1 说明   用J-link来烧写Bootloader,Pixhawk板FMU/IO接口说明:      J-link接口说明:      Pix ...

  2. [leetcode][math] Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  3. php使用flock堵塞写入文件和非堵塞写入文件

    php使用flock堵塞写入文件和非堵塞写入文件 堵塞写入代码:(全部程序会等待上次程序运行结束才会运行,30秒会超时) <?php $file = fopen("test.txt&q ...

  4. jni传递对象中包含arraylist对象。

    相信在使用jni的过程中,总是要传递各种各样的类型,在这其中,我也碰到了一些问题. 简单的传一些内容,相信在网上一搜一大把. 所以我们就来说说.传递对象中包含arraylist吧. 在这里先给大家一个 ...

  5. 语法错误: unexpected ''); ?></span></span></h2> ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'

    语法错误: unexpected ''); ?></span></span></h2>' (T_CONSTANT_ENCAPSED_STRING), expe ...

  6. 如何解读「量子计算应对大数据挑战:中国科大首次实现量子机器学习算法」?——是KNN算法吗?

    作者:知乎用户链接:https://www.zhihu.com/question/29187952/answer/48519630 我居然今天才看到这个问题,天……本专业,有幸听过他们这个实验的组会来 ...

  7. 32.AngularJS 表达式

    转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 表达式写在双大括号内:{{ expression }}. AngularJS 表达式把数据 ...

  8. 《剑指offer》调整数组顺序使奇数位于偶数前面

    一.题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 二.输入描述 ...

  9. springMVC学习笔记_转载(一)-----springMVC原理

    阅读目录 一.什么是springmvc 二.mvc在b/s系统下的应用 三.SpringMVC框架介绍 回到顶部 一.什么是springmvc springMVC是spring框架的一个模块,spri ...

  10. Xor Sum 2(位运算)

    D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...