Bond UVA - 11354(LCA应用题)
Once again, James Bond is on his way to saving the world. Bond's latest mission requires him to travel between several pairs of cities in a certain country.
The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads. Bond is going to steal a vehicle, and drive along the roads from city s to city t. The country's police will be patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the police.
More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is defined as the maximum dangerousness of any road on this path.
Now, it's your job to help Bond succeed in saving the world by finding the least dangerous paths for his mission.
Input
There will be at most 5 cases in the input file.
The first line of each case contains two integers N, M (2 ≤ N≤ 50000, 1≤ M ≤ 100000) – number of cities and roads. The next M lines describe the roads. The i-th of these lines contains three integers: xi, yi, di (1 ≤ xi, yi ≤ N, 0 ≤ di ≤ 109) - the numbers of the cities connected by the ith road and its dangerousness.
Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by Q lines, the i-th of which contains two integers si and ti (1 ≤ si, ti ≤ N, si !=ti).
Consecutive input sets are separated by a blank line.
Output
For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between cities si and ti. Consecutive output blocks are separated by a blank line.
The input file will be such that there will always be at least one valid path.
LCA实际应用的一个题目
题意:
N个城市 M条路 保证可以联通 ,有q个询问,求出从x走到y的最短路径上的最大边长
如果对于LCA还有什么不理解的话,可以看我的上一篇随笔,LCA倍增算法详解。
如果还有不懂可以留言给我
根据题意应该先生成一个最小生成树,然后通过LCA求出最短路径上的最大边长;
此题对于格式卡的非常死。注意格式
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include <iomanip>
using namespace std;
const int maxn=;
const int INF=;
struct node1 {
int u,v,f;
}qu[maxn*];
struct node2 {
int x,y;
node2 (int x= ,int y=):x(x),y(y){};
};
vector<node2>a[maxn];
int n,m,pre[maxn],fa[maxn],rk[maxn],cost[maxn];
int anc[maxn][],maxcost[maxn][];
int cmp1(node1 a,node1 b){
return a.f<b.f;
}
int find(int x){
while(x!=pre[x]) x=pre[x];
return x;
}
void dfs(int u,int f,int depth){
rk[u]=depth;
fa[u]=u;
int len=a[u].size();
for (int i= ;i<len ;i++){
int v=a[u][i].x;
int w=a[u][i].y;
if (v!=f) {
dfs(v,u,depth+);
fa[v]=u;
cost[v]=w;
}
}
}
void lca(){
for (int i= ;i<=n ;i++){
anc[i][]=fa[i];
maxcost[i][]=cost[i];
for (int j= ;(<<j)<=n ;j++){
anc[i][j]=-;
}
}
for (int j= ;(<<j)<=n ;j++){
for (int i= ;i<=n ;i++){
if (anc[i][j-]!=-) {
anc[i][j]=anc[anc[i][j-]][j-];
maxcost[i][j]=max(maxcost[i][j-],maxcost[anc[i][j-]][j-]);
}
}
}
}
int query(int x,int y){
if (rk[x]<rk[y]) swap(x,y);
int k,ans=-INF;
for (k= ;(<<(+k))<=rk[x] ;k++) ;
for (int i=k ;i>= ;i--){
if (rk[x]-(<<i)>=rk[y]) {
ans=max(ans,maxcost[x][i]);
x=anc[x][i];
}
}
if (x==y) return ans;
for (int i=k ;i>= ;i--){
if (anc[x][i]!=- && anc[x][i]!=anc[y][i] ){
ans=max(ans,maxcost[x][i]);
x=anc[x][i];
ans=max(ans,maxcost[y][i]);
y=anc[y][i];
}
}
ans=max(ans,max(cost[x],cost[y]));
return ans;
}
int main() {
int flag= ;
while(scanf("%d%d",&n,&m)!=EOF){
if (flag==) printf("\n");
flag=;
for (int i= ;i<m ;i++)
scanf("%d%d%d",&qu[i].u,&qu[i].v,&qu[i].f);
sort(qu,qu+m,cmp1);
for (int i= ;i<=n ;i++ ){
a[i].clear();
pre[i]=i;
}
for (int i= ;i<m ;i++){
int x=find(qu[i].u);
int y=find(qu[i].v);
if (x!=y) {
pre[x]=y;
a[x].push_back(node2(y,qu[i].f));
a[y].push_back(node2(x,qu[i].f));
}
}
dfs(,-,);
lca();
int q;
scanf("%d",&q);
while(q--){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",query(x,y));
}
}
return ;
}
Bond UVA - 11354(LCA应用题)的更多相关文章
- Uva 11354 LCA 倍增祖先
题目链接:https://vjudge.net/contest/144221#problem/B 题意:找一条从 s 到 t 的路,使得瓶颈路最小. 点的数目是10^4,如果向之前的方案求 maxc ...
- Bond UVA - 11354(并查集按秩合并)
题意: 给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路. 解析: 应该都想过用prime的次小生成树做..但二维数组开不了那么大..所以只能用kruskal了.... #incl ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- uva 11354 - Bond(树链拆分)
题目链接:uva 11354 - Bond 题目大意:给定一张图.每次询问两个节点路径上进过边的危急值的最大值的最小值. 解题思路:首先建立最小生成数,然后依据这棵树做树链剖分. #include & ...
- UVA 11354 Bond(MST + LCA)
n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...
- UVA 11354 Bond 最小生成树 + lca
题意 给出一张图,q个询问,每次询问给出uv,找出一条路径,使这条路径上的最大边权是两点所有路径中最小,输出这个值 思路 很显然要先求出最小生成树,任意两点在最小生成树上有唯一路径,并且这条路径上的最 ...
- UVA - 11354 Bond(最小生成树+LCA+瓶颈路)
题意:N个点,M条路,每条路的危险度为路上各段中最大的危险度.多组询问,点s到点t的所有路径中最小的危险度. 分析: 1.首先建个最小生成树,则s到t的路径一定是危险度最小的. 原因:建最小生成树的最 ...
- UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)
题意: n个城市,m条路,每条路有个危险值,要使得从s走到t的危险值最小.回答q个询问,每个询问有s和t,要求输出从s到t最小的危险值.(5万个点,10万条边) 思路: 其实要求的是任意点对之间的最小 ...
- UVA 11354 Bond(最小瓶颈路+倍增)
题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...
随机推荐
- Maven json包找不到解决办法
在Maven中央仓库找到Maven的jar <dependency> <groupId>net.sf.json-lib</groupId> <artifact ...
- CentOS7上安装并配置Nginx、PHP、MySql
一.Nginx 1.安装nginx yum install nginx 2.启动nginx systemctl start nginx 除了systemctl start nginx之外,常用的相关命 ...
- SpringBoot Hello World
本文首发于我的github博客 前言 SpringBoot是Spring MVC升级版,基于『约定优于配置』的原则,快速开发出web程序. 环境 本系列笔记环境如下: Sun JDK1.8.0_20 ...
- uva12325 暴力枚举
这题刚开始我就贪心,直接wrong了,贪心适合可以取一个物体部分的题. 还是老实枚举吧,注意枚举要分类,不然可能会超时,还有注意答案是long long AC代码: #include<cstdi ...
- filebeat.yml(中文配置详解)
################### Filebeat Configuration Example ######################### ####################### ...
- 关于.Net的知识和相关书籍
a. DBCC DROPCLEANBUFFERS 清空缓存信息b. DBCC FREEPROCCACHE 从过程缓存中删除所有元素2. 引用两个和尚打水的故事,说明平时要注重积累,只有量变达到了才会形 ...
- 从 PHP 到 Java
* { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...
- apue.h头文件(UNIX环境高级编程)
在看UNIX环境高级编程是,碰到一个头文件"apue.h",搜一下别人的帖子,其实apue.h是作者自己写的一个文件,包含了常用的头文件,系统不自带.其中包含了常用的头文件,以及出 ...
- 【Unity3D】Unity3D开发《我的世界》之三、创建一个Chunk
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/unity_minecraft_03.html 一.引入int类型的Vector3 我们都知道Unity3D里Vec ...
- 一句python,一句R︱模块导入与查看、数据读写出入、数据查看函数、数据类型、遍历文件
先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. pandas中有类似R中的read.table的功能,而 ...