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 ;
}

最新文章

  1. a标签填充父容器
  2. Requests 乱码
  3. JSP网站开发基础总结《五》
  4. iOS 解决导航栏左右 BarButtonItem偏移位置的问题
  5. URL格式编码与解码
  6. Joomla的在线视频播放插件:AllVideos
  7. android 中 ColorDrawable dw = new ColorDrawable(0x3ccccccc),关于颜色定义的总结
  8. javascript-Cookie的应用
  9. java.lang.IllegalAccessError: class javax.activation.SecuritySupport12 cannot access its superclass
  10. UVA11100- The Trip, 2007
  11. c语言下多线程
  12. 温故而知新——map
  13. Python[小甲鱼008了不起的分支和循环2]
  14. Springboot学习记录1--概念介绍以及环境搭建
  15. 快速了解 Robot Operating System(ROS) 机器人操作系统
  16. 移动设备(手机)的唯一ID有哪些
  17. Jmeter+ant+jenkins集成
  18. SVN插件和Tomcat插件地址
  19. Spring中集合注入方法
  20. day3 zookeeper

热门文章

  1. 将ubuntu安装在用剩下的硬盘改装成的移动硬盘时遇到的问题及解决办法
  2. spring cloud集成 consul源码分析
  3. kafka集群安装配置
  4. 机器学习规则:ML工程最佳实践----rule_of_ml section 3【翻译】
  5. DefaultView 的作用(对DataSet查询出的来数据进行排序)
  6. 认识javascript的引擎之--1
  7. HDU 5223 GCD
  8. NodeJS学习笔记 进阶 (3)Nodejs 进阶:Express 常用中间件 body-parser 实现解析(ok)
  9. vue项目的环境变量
  10. Lenovo k860i 移植Android 4.4 cm11进度记录【下篇--实时更新中】