2018.09.24 codeforces 1051F. The Shortest Statement(dijkstra+lca)
传送门
这真是一道一言难尽的题。
首先比赛的时候居然没想出来正解。
其次赛后调试一直调不出来最后发现是depth传错了。
其实这是一道简单题啊。
对于树边直接lca求距离。
由于非树边最多21条。
因此我们对这21条边连接的42个点都跑一次最短路来更新答案的最小值即可。
代码:
#include<bits/stdc++.h>
#define N 100005
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define fi first
#define se second
using namespace std;
inline ll read(){
ll ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
int n,m,q,st[N][21],first[N],stk[N],dep[N],cnt=0,tot=0,top=0;
ll w[N],dis[45][N];
bool vis[N];
struct edge{int v,next;ll w;}e[N<<1];
inline void add(int u,int v,ll w){e[++cnt].v=v,e[cnt].next=first[u],e[cnt].w=w,first[u]=cnt;}
inline void dfs(int p,int fa){
vis[p]=true;
for(int i=1;i<=20;++i)st[p][i]=st[st[p][i-1]][i-1];
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(v==fa)continue;
if(!vis[v])w[v]=w[p]+e[i].w,st[v][0]=p,dep[v]=dep[p]+1,dfs(v,p);
else stk[++top]=p,stk[++top]=v;
}
}
inline int lca(int x,int y){
if(dep[x]<dep[y])x^=y,y^=x,x^=y;
int tmp=dep[x]-dep[y];
for(int i=20;~i;--i)if(tmp>>i&1)x=st[x][i];
if(x==y)return x;
for(int i=20;~i;--i)if(st[x][i]!=st[y][i])x=st[x][i],y=st[y][i];
return st[x][0];
}
inline void dijkstra(int s,int siz){
priority_queue<pli,vector<pli>,greater<pli> >q;
for(int i=1;i<=n;++i)dis[siz][i]=1e18,vis[i]=false;
q.push(pli(dis[siz][s]=0,s));
while(!q.empty()){
int x=q.top().se;
q.pop();
if(vis[x])continue;
vis[x]=true;
for(int i=first[x];i;i=e[i].next){
int v=e[i].v;
if(dis[siz][v]>dis[siz][x]+e[i].w)dis[siz][v]=dis[siz][x]+e[i].w,q.push(pli(dis[siz][v],v));
}
}
}
int main(){
n=read(),m=read();
for(int i=1;i<=m;++i){
int u=read(),v=read();
ll w=read();
add(u,v,w),add(v,u,w);
}
dfs(1,0);
sort(stk+1,stk+top+1),top=unique(stk+1,stk+top+1)-stk-1;
for(int i=1;i<=top;++i)dijkstra(stk[i],i);
q=read();
while(q--){
int u=read(),v=read();
ll ans=w[u]+w[v]-2*w[lca(u,v)];
for(int i=1;i<=top;++i)ans=min(ans,dis[i][u]+dis[i][v]);
cout<<ans<<'\n';
}
return 0;
}
2018.09.24 codeforces 1051F. The Shortest Statement(dijkstra+lca)的更多相关文章
- 2018.09.24 codeforces 1053C. Putting Boxes Together(线段树)
传送门 就是让你维护动态的区间带权中位数. 然而昨晚比赛时并没有调出来. 想找到带权中位数的中点可以二分(也可以直接在线段树上找). 也就是二分出第一个断点,使得断点左边的和恰好大于或等于断点右边的和 ...
- 2018.09.24 bzoj1867: [Noi1999]钉子和小球(概率dp)
传送门 概率dp经典题. 如果当前位置(i,j)(i,j)(i,j)有钉子,那么掉到(i+1,j),(i+1,j+1)(i+1,j),(i+1,j+1)(i+1,j),(i+1,j+1)的概率都是1/ ...
- codeforces 1051F The Shortest Statement
题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...
- [Codeforces 1051F] The Shortest Statement 解题报告(树+最短路)
题目链接: https://codeforces.com/contest/1051/problem/F 题目大意: 给出一张$n$个点,$m$条边的带权无向图,多次询问,每次给出$u,v$,要求输出$ ...
- Codeforces.1051F.The Shortest Statement(最短路Dijkstra)
题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...
- 2018.09.24 bzoj4977: [[Lydsy1708月赛]跳伞求生(贪心+线段树)
传送门 线段树好题. 这题一看我就想贪心. 先把a,b数组排序. 然后我们选择a数组中最大的b个数(不足b个就选a个数),分别贪心出在b数组中可以获得的最大贡献. 这时可以用线段树优化. 然后交上去只 ...
- 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)
传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...
- 2018.12.12 codeforces 931E. Game with String(概率dp)
传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...
- 2018.09.27 bzoj3029: 守卫者的挑战(概率dp)
传送门 概率dp经典题目. 直接f[i][j][k]f[i][j][k]f[i][j][k]表示当前是第i次挑战,已经胜利了j次,目前的背包剩余空间是k. 然后用前面的转移后面的就行了. 注意第三维可 ...
随机推荐
- Activity工作流学习(一)——Activity服务类
Activity有9个service1.DynamicBpmnService动态Bpmn服务Service providing access to the repository of process ...
- mysql改数据库名称
第一种方法: 1.创建需要改成新名的数据库.2.mysqldum 导出要改名的数据库3.删除原来的旧库(确定是否真的需要)当然这种方法虽然安全,但是如果数据量大,会比较耗时,哎,当时连这种方法都没有想 ...
- 【转】UNITY之LUA加密
来自:Lua加密 两种方式:一种用luac,一种用luajit luac加密: 1.lua本身可以使用luac将脚本编译为字节码(bytecode)从而实现加密,去官网下载Lua源代码包(http:/ ...
- 富文本编辑器-UEditor
官方网址:http://ueditor.baidu.com/website/index.html 下载地址:http://ueditor.baidu.com/website/download.html ...
- 在Eclipes中查看源代码和大纲快速定位
1 在Eclipes中查看源代码,快捷键使用clrl+光标,选择你要查看的方法和属性查看源代码.例如你想看StringBuilder这个类源代码 StringBuilder allow = new S ...
- How a non-windowed component can receive messages from Windows
Why do it? Sometimes we need a non-windowed component (i.e. one that isn't derived fromTWinControl) ...
- whlie and for
public class TestWhileAndFor { /**测试 while和for循环练习 * 100 以内的奇数和偶数的和 * @author Administrator * */ pub ...
- Python内置类型性能分析
Python内置类型性能分析 timeit模块 timeit模块可以用来测试一小段Python代码的执行速度. class timeit.Timer(stmt='pass', setup='pass' ...
- JAVA数据类型(转载)
JAVA中值类型的只有short,char,byte,int,long,double,float,boolean八大基本类型,其他的所有类型都是引用类型. 首先我们都知道在编程中赋值运算“=”的意思是 ...
- Unable to get the default Bean Validation factory
前几天看了一下教程 ,自己试着配置了一下web下的hibernate,悲剧的时,出错了提示下面: 信息: Hibernate Validator bean-validator-3.0-JBoss-4. ...