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 ...
随机推荐
- FortiGate防火墙内存使用率高问题
1.现象:zabbix监控到防火墙内存使用率频繁超过80%,而FortiGate防火墙内存超过80%将开启自身保护模式而不能新加策略等. 2.分析:这种情况一般是某些进程再释放内存的时候卡住.可以先查 ...
- redis(三)积累-基本的取值和设值
1. 先把redis的连接池拿出来, JedisPool pool=new JedisPool(new JedisPoolConfig(),"127.0.0.1") Jedis ...
- task 定时设置
每天凌晨2点 0 0 2 * * ?和每天隔一小时 0 * */1 * * ? 例1:每隔5秒执行一次:*/5 * * * * ? 例2:每隔5分执行一次:0 */5 * * * ? 在26分.29 ...
- iOS 证书申请新步骤
2018iOS完整的证书申请和打包过程 - 简书https://www.jianshu.com/p/2b3c2693f4f2
- Jquery的Ajax中contentType和dataType的区别
$.ajax({ type: httpMethod, cache:false, async:false, contentType: "application/json; charset=ut ...
- Excel怎么下拉框多选
打开Exlce, 确定,然后 右击查看代码,把这段代码复制到新建的文件里面 此时Excel会给出提示,选择否,,系统会提示保存,在保存的时候选择启用宏的工作簿然后保存,此时Excel下拉框多选就搞定了 ...
- Exploring the world of Android :: Part 2
September 17th, 2009 by Tom van Zummeren | And I’m back! Reporting live on the glorious adventures i ...
- JS如何获取上传标签的文件路径和文件名?
如何使用JS获取type="file"的标签上传文件的文件路径及文件名: 代码: <!doctype html><html lang="en" ...
- 网站文章分享到微博,微信、qq空间--举例用织梦
有对应接口的,传递参数过去就行了,注意下面的{..},需要替换为你的内容,看参数名称你应该知道是什么东东 新浪地址接口http://service.weibo.com/share/share.php? ...
- php 多进程
php 在使用场景中一般是处理web应用,所以多进程使用不适合在web中使用,且php-fpm中pcntl_fork不能使用,所以使用场景是在cgi模式下 一个进程调用pcntl_fork函数后,系统 ...