gym 101081 gym F. Auction of Services 最小生成树+倍增LCA
2.0 s
256 MB
standard input
standard output
It is becoming more common to use strategies based on social networks for shopping, contract services, arrange meetings, etc. Such strategies often involve interesting mathematical knowledge, like Game Theory. A new trend has appeared, the use of social networks to carry out virtual auctions to contract services. The service providers have agreements between them that enforce a service to cost a predetermine value. The prices a company charges, when it works in collaboration with each of its partners, can be very different, since many factors such as location, previous partnerships, tradition, etc. influence the convenience of a particular partnership.
In this problem you want to estimate the price of a service given by a couple of service providers. It is already known the price of the service given by some companies that have an agreement to work together. Furthermore, if we have two companies A and B, we can contract a service (offered by them) using a chain of companies c1, c2, ... ck, such that
- ci and ci + 1 have an agreement, for i = 1, ..., k - 1,
- c1 = A and ck = B.
Also, we know that the price to contract such chain of companies, is the highest price of a service given by a pair of adjacent companies in that chain. That's the power of social networks!

In the previous figure, despite the fact that the price of the agreement between A and C is 13, the best price (in the network that links these companies) is 9, using the agreements through B.
Your task is to help these virtual auctions. The person interested in contracting a certain service chooses a pair of companies he want to contract, and you need to make a program that answers the minimum possible price of that service.
The first line contains two integers N and M, the number of companies and agreements, respectively. The companies are numbered from 1 to N. Each of the following M lines contains three integers, the ith contains Ai, Bi and Ci, indicating that there is an agreement between companies Ai and Bi whose price is Ci. Each pair of companies has at most one agreement. It is guaranteed it is possible to contract a service between any pair of companies.
The next line contains an integer Q, the number of queries. The following Q lines contain a pair of integers representing the companies which we want to contract.
Limits
- 2 ≤ N ≤ 105
- 1 ≤ M ≤ 2·105
- 1 ≤ Ai ≠ Bi ≤ N
- 1 ≤ Ci ≤ 106
- 1 ≤ Q ≤ 2·105
Print Q integers representing the price of the service for each query.
4 4
1 2 6
1 3 1
2 4 2
3 4 2
2
1 2
1 4
2
2
4 4
1 2 1
2 3 7
2 4 3
3 4 4
4
1 3
1 4
3 2
4 2
4
3
4
3
题意:无向带权图,找一个点到另一个点的边权最大值最小;
思路:最小生成树:要在n个城市之间铺设光缆,主要目标是要使这 n 个城市的任意两个之间都可以通信,但铺设光缆的费用很高,且各个城市之间铺设光缆的费用不同,因此另一个目标是要使铺设光缆的总费用最低。这就需要找到带权的最小生成树。
显然先构造一颗最小生成树,然后类似LCA的求法求两个点之间的最大值;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<bitset>
#include<set>
#include<map>
#include<time.h>
using namespace std;
#define LL long long
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=,MOD=;
const double eps=1e-,pi=(*atan(1.0));
struct is
{
int u,v,w;
bool operator <(const is &c)const
{
return w<c.w;
}
}a[N<<];
int faT[N];
int Find(int x)
{
return x==faT[x]?x:faT[x]=Find(faT[x]);
}
vector<pair<int,int> >edge[N];
int fa[N][],ma[N][],deep[N];
void dfs(int u,int fat)
{
for (int i=; i<= ;i++) {
if(deep[u]<(<<i)) break;
fa[u][i] = fa[fa[u][i-]][i-];
ma[u][i] = max(ma[fa[u][i-]][i-],ma[u][i-]);
}
for (int i=;i<edge[u].size();i++)
{
int v=edge[u][i].first;
int w=edge[u][i].second;
if(v==fat) continue;
deep[v]=deep[u]+;
fa[v][]=u;
ma[v][]=w;
dfs(v,u);
}
}
int RMQ_LCA(int x,int y) {
if(deep[x]<deep[y]) swap(x,y);
int d=deep[x]-deep[y],ans=;
for (int i=; i<= ;i++)
if((<<i)&d) ans=max(ans,ma[x][i]),x=fa[x][i];
for (int i=; i>= ;i--) {
if(fa[x][i]!=fa[y][i]) {
ans=max(ans,ma[x][i]),ans=max(ans,ma[y][i]);
x=fa[x][i];y=fa[y][i];
}
}
if(x==y) return ans;
else return max(ans,max(ma[y][],ma[x][]));
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
faT[i]=i;
for(int i=;i<=m;i++)
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
sort(a+,a++m);
for(int i=;i<=m;i++)
{
int x=Find(a[i].u);
int z=Find(a[i].v);
if(x!=z)
{
edge[a[i].u].push_back(make_pair(a[i].v,a[i].w));
edge[a[i].v].push_back(make_pair(a[i].u,a[i].w));
faT[x]=z;
}
}
dfs(,);
int q;
scanf("%d",&q);
while(q--)
{
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",RMQ_LCA(u,v));
}
return ;
}
gym 101081 gym F. Auction of Services 最小生成树+倍增LCA的更多相关文章
- 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集
[题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...
- 【bzoj3732】Network 最小生成树+倍增LCA
题目描述 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 & ...
- 【bzoj4242】水壶 BFS+最小生成树+倍增LCA
题目描述 JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有P个,编号为1...P. JOI君只能进入 ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- BFS+最小生成树+倍增+LCA【bzoj】4242 水壶
[bzoj4242 水壶] Description JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有 ...
- BZOJ 3732 Network —— 最小生成树 + 倍增LCA
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 <= N <= 15, ...
- codeforce 378 div 2 F —— Drivers Dissatisfaction (最小生成树,LCA,倍增)
官方题解: If you choose any n - 1 roads then price of reducing overall dissatisfaction is equal to min(c ...
- LOJ #2876. 「JOISC 2014 Day2」水壶 BFS+最小生成树+倍增LCA
非常好的一道图论问题. 显然,我们要求城市间的最小生成树,然后查询路径最大值. 然后我们有一个非常神的处理方法:进行多源 BFS,处理出每一个城市的管辖范围. 显然,如果两个城市的管辖范围没有交集的话 ...
- codevs 1519 过路费 最小生成树+倍增
/*codevs 1519 过路费 最小生成树+倍增*/ #include<iostream> #include<cstdio> #include<cstring> ...
随机推荐
- tuple的基本使用
常用如下所示: # 元祖,tuple-->不可变,但元祖里面的数据可以变化 # 当元祖只有一个元素时,需加’,‘,否则类型是<class 'int'> # tuple1 = (1) ...
- SyntaxError:unexpected EOF while parsing(<string,line 0>)
在python+Django中出现报错:(上图) 经断点发现:python内置函数eval在处理空字符串时会返回EOF错误,判断后解决
- 目标检测(六)YOLOv2__YOLO9000: Better, Faster, Stronger
项目链接 Abstract 在该论文中,作者首先介绍了对YOLOv1检测系统的各种改进措施.改进后得到的模型被称为YOLOv2,它使用了一种新颖的多尺度训练方法,使得模型可以在不同尺寸的输入上运行,并 ...
- loj2876 水壶 [JOISC 2014 Day2] kruscal重构树
正解:kruscal重构树+bfs 解题报告: 我永远喜欢loj! 感觉这题和这题挺像的,,,预处理和解题方法都是,,,所以大概整体二分能过去? 但因为做这题主要是入门一下kruscal重构树,,,所 ...
- array_merge() 用法
array_merge() 函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的).如果数组是数字索引的,则键名会以连续方式重新索引. 注释:如果仅 ...
- (转)Golang--使用iota(常量计数器)
iota是golang语言的常量计数器,只能在常量的表达式中使用. iota在const关键字出现时将被重置为0(const内部的第一行之前),const中每新增一行常量声明将使iota计数一次(io ...
- 【UML】NO.53.EBook.5.UML.1.013-【UML 大战需求分析】- 组合结构图(Composition Structure Diagram)
1.0.0 Summary Tittle:[UML]NO.52.EBook.1.UML.1.012-[UML 大战需求分析]- 交互概览图(Interaction Overview Diagram) ...
- Java读取resource文件/路径的几种方式
方式一: String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//获取文 ...
- MTSC2019第五届中国移动互联网测试开发大会北京站震撼来袭!
MTSC2019 暨第五届中国移动互联网测试开发大会(Mobile Testing Summit China)是由国内最大的测试开发技术社区之一 TesterHome 发起的行业会议,聚焦于软件测试及 ...
- Docker:Windows7下使用docker toolbox(1)
一.安装 官方网址:https://docs.docker.com/docker-for-windows/install/ win10以下安装:https://www.docker.com/produ ...