Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
https://codeforces.com/contest/1051/problem/F
题意
给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路
其中 m-n<=20,1<=n,m<=1e5
思路
- 因为图一定联通,所以n-1<=m<=n+20
- 因为是求任意两点的最短路,所以直接暴力跑最短路是不行的,考虑选择性的跑最短路
- 因为是求两点之间的距离,所以可以往lca方面想
- 先跑一棵生成树出来,然后处理出每个点的lca,这样就可以求出任意两点的距离
- 然后就可以记录下剩下的边,因为剩下的边会影响经过连接的两个点的最短路,那么可以对这些边的两个点跑一次dij
- 然后对于没次查询的两个点
ans=min(d[u]+d[v]-2*d[lca(u,v)],min(d[1tot][u]+d[1tot][v]))
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define ft first
#define se second
#define pb push_back
#define M 100005
#define inf 1e15
#define pii pair<ll,int>
using namespace std;
vector<pii>g[M];
int E[M],vi[M],f[M][30],D[M],inq[M];
ll dp[M],d[50][M];
int n,m,u,v,w,i,q,tot=0;
ll ans;
void dfs(int u,int fa){
vi[u]=1;
f[u][0]=fa;for(int i=1;i<=20;i++)f[u][i]=f[f[u][i-1]][i-1];
for(int i=0;i<g[u].size();i++){
int v=g[u][i].se;ll w=g[u][i].ft;if(v==fa)continue;
if(!vi[v]){
D[v]=D[u]+1;dp[v]=dp[u]+w;
dfs(v,u);
}else{
E[tot++]=u;E[tot++]=v;
}
}
return ;
}
void dj(int id,int st){
priority_queue<pii,vector<pii>,greater<pii> >Q;
Q.push(mk(0,st));
for(int i=1;i<=n;i++){d[id][i]=inf;inq[i]=0;}
d[id][st]=0;
while(!Q.empty()){
int u=Q.top().se;Q.pop();
if(inq[u])continue;
inq[u]=1;
for(int i=0;i<g[u].size();i++){
int v=g[u][i].se;ll w=g[u][i].ft;
if(d[id][v]>d[id][u]+w){
d[id][v]=d[id][u]+w;
Q.push(mk(d[id][v],v));
}
}
}
}
int lca(int u,int v){
if(D[v]>D[u])swap(u,v);
int dis=D[u]-D[v];
for(int i=20;i>=0;i--)if(dis&(1<<i))u=f[u][i];
if(u==v)return u;
for(int i=20;i>=0;i--)
if(f[u][i]!=f[v][i]){u=f[u][i];v=f[v][i];}
return f[u][0];
}
int main(){
cin>>n>>m;
for(i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
g[u].pb(mk(w,v));
g[v].pb(mk(w,u));
}
dfs(1,0);
sort(E,E+tot);tot=unique(E,E+tot)-E;
for(i=0;i<tot;i++)dj(i,E[i]);
cin>>q;
while(q--){
scanf("%d%d",&u,&v);
ans=dp[u]+dp[v]-2*dp[lca(u,v)];
for(i=0;i<tot;i++)
ans=min(ans,d[i][u]+d[i][v]);
printf("%lld\n",ans);
}
}
Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)的更多相关文章
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路
F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...
- 2018.9.20 Educational Codeforces Round 51
蒟蒻就切了四道水题,然后EF看着可写然而并不会,中间还WA了一次,我太菜了.jpg =.= A.Vasya And Password 一开始看着有点虚没敢立刻写,后来写完第二题发现可以暴力讨论,因为保 ...
- Educational Codeforces Round 51 (Rated for Div. 2)
做了四个题.. A. Vasya And Password 直接特判即可,,为啥泥萌都说难写,,,, 这个子串实际上是忽悠人的,因为每次改一个字符就可以 我靠我居然被hack了???? %……& ...
- Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement
题目链接:The Shortest Statement 今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路.看来这种题还挺常见的. 为什么最终答案要从42个点的最短路(到$x,y$) ...
- CodeForces Educational Codeforces Round 51 (Rated for Div. 2)
A:Vasya And Password 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen(&q ...
- 【 Educational Codeforces Round 51 (Rated for Div. 2) F】The Shortest Statement
[链接] 我是链接,点我呀:) [题意] [题解] 先处理出来任意一棵树. 然后把不是树上的边处理出来 对于每一条非树边的点(最多21*2个点) 在原图上,做dijkstra 这样就能处理出来这些非树 ...
- The Shortest Statement(Educational Codeforces Round 51 (Rated for Div.2)+最短路+LCA+最小生成树)
题目链接 传送门 题面 题意 给你一张有\(n\)个点\(m\)条边的联通图(其中\(m\leq n+20)\),\(q\)次查询,每次询问\(u\)与\(v\)之间的最短路. 思路 由于边数最多只比 ...
- Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)
F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...
随机推荐
- 从零开始写bootloader(2)
下图是设置内核启动参数的存放图示,由于bootloader启动内核时,需要给内核传输一些启动参数,但是由于当bootloader把内核 启动之后,程序就跳转到内核中执行了,再也不会回到bootload ...
- 解决在Mac的Vmware Fusion中装win7系统和mac原生系统直接切换win7系统分辨率变化的问题
虚拟机 - 设置 - 显示屏 - 全屏显示retina (此选项钩去掉)
- JAVA EXAM3 复习提纲
[Practice11_Zipcode_ArrayList] Zipcode class: //3 variables: zipcode, city, county, and compare by c ...
- width多少,超过了用....表示
maxWidth:'140px',whiteSpace:'nowrap',overflow:'hidden',textOverflow:'ellipsis'
- Wiki服务器的配置
本文介绍在Ubuntu Server 上配置Wiki服务器的MediaWiki 官方参考, 所用的版本是 Ubuntu 16.04. 安装必要的软件 通过命令 sudo netstat -tulpn ...
- 如何从应用直接跳转AppStore 电话 短信 邮件
//如何从应用直接跳转AppStore [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"应用程序的下载链接& ...
- 线特征---LineMatching代码运行(五)
[1] https://github.com/dlut-dimt/LineMatching The code is based on Matlab. https://github.com/ka ...
- webapi 设置不显示接口到swaggerUI
请添加如下属性: [ApiExplorerSettings(IgnoreApi = true)]
- axios基本使用
1,安装axios cnpm install axios --save 2.在main.js里面引入 import axios from 'axios' /*解决在其他组件中不能用*/Vue.prot ...
- 5F - Coin Change
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make c ...