首页 技术 正文
技术 2022年11月7日
0 收藏 845 点赞 311 浏览 3304 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900