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 ...
随机推荐
- Codeforces Beta Round #55 (Div. 2)
Codeforces Beta Round #55 (Div. 2) http://codeforces.com/contest/59 A #include<bits/stdc++.h> ...
- Spring Boot 使用465端口发送邮件
2017年10月27日 15:04:24 伊宇紫 阅读数:2710 标签: 465端口邮件springboot 更多 个人分类: Java 版权声明:本文为博主原创文章,未经博主允许不得转载. h ...
- SQL Server 触发器 表的特定字段(一个字段)更新时,触发Update触发器
CREATE TRIGGER [dbo].[Trg_Update_table1_column1] on table1 after update as if update (column1) ...
- [leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- cmd乱码问题
1.进入 cmd 窗口 2.字符编码切换回中文:chcp 936 MS-DOS为以下国家和语言提供字符集: 代码页描述 1258 越南语 1257 波罗的语 1256 阿拉伯语 1255 希 ...
- HttpClient上传文件
1.上传客户端代码: public static void upload() { CloseableHttpClient httpclient = HttpClients.createDefault( ...
- JSP中的数据库操作,MySQL基础操作(一)
一.JDBC JDBC(java data base concectivity),是一种用于执行SQL语句的java API,可以为多种关系库提供统一访问. 通常使用JDBC完成以下操作: 1)同数据 ...
- 云计算之 PaaS详解
PaaS是Platform-as-a-Service的缩写,意思是平台即服务. Paas - 概述 计算机技术 PaaS(Platform-as-a-Service:平台即服务) 全称:(Platfo ...
- 递归函数 day17
一 递归函数 n = 1 金老板 38+2 =40n = 2 alex n+2= 金老板 36+2 = 38n = 3 wusir n+2 = alex wusir 36 def age(n): #n ...
- Python 字符串(center)
center 描述 Python center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串.默认填充字符为空格. 语法 center()方法语法: str.center(w ...