传送门

这真是一道一言难尽的题。

首先比赛的时候居然没想出来正解。

其次赛后调试一直调不出来最后发现是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)的更多相关文章

  1. 2018.09.24 codeforces 1053C. Putting Boxes Together(线段树)

    传送门 就是让你维护动态的区间带权中位数. 然而昨晚比赛时并没有调出来. 想找到带权中位数的中点可以二分(也可以直接在线段树上找). 也就是二分出第一个断点,使得断点左边的和恰好大于或等于断点右边的和 ...

  2. 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/ ...

  3. codeforces 1051F The Shortest Statement

    题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...

  4. [Codeforces 1051F] The Shortest Statement 解题报告(树+最短路)

    题目链接: https://codeforces.com/contest/1051/problem/F 题目大意: 给出一张$n$个点,$m$条边的带权无向图,多次询问,每次给出$u,v$,要求输出$ ...

  5. Codeforces.1051F.The Shortest Statement(最短路Dijkstra)

    题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...

  6. 2018.09.24 bzoj4977: [[Lydsy1708月赛]跳伞求生(贪心+线段树)

    传送门 线段树好题. 这题一看我就想贪心. 先把a,b数组排序. 然后我们选择a数组中最大的b个数(不足b个就选a个数),分别贪心出在b数组中可以获得的最大贡献. 这时可以用线段树优化. 然后交上去只 ...

  7. 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)

    传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...

  8. 2018.12.12 codeforces 931E. Game with String(概率dp)

    传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...

  9. 2018.09.27 bzoj3029: 守卫者的挑战(概率dp)

    传送门 概率dp经典题目. 直接f[i][j][k]f[i][j][k]f[i][j][k]表示当前是第i次挑战,已经胜利了j次,目前的背包剩余空间是k. 然后用前面的转移后面的就行了. 注意第三维可 ...

随机推荐

  1. JSON.Stringify()和JSON.parse()的比较使用

    1.  JSON.Stringify() 将一个对象解析成字符串 <script> function myonclick() { var value = $('select option: ...

  2. Overriding managed version XX for YY

    在警告部分,添加<!--$NO-MVN-MAN-VER$-->. <build> <plugins> <plugin> <groupId>o ...

  3. margin-top和padding-top

    padding- top 在原有的基础上进一步的扩张 margin - top 在原有的位置上发生上下的平移 <!DOCTYPE html> <html lang="en& ...

  4. Flume环境搭建_五种案例(转)

    Flume环境搭建_五种案例 http://flume.apache.org/FlumeUserGuide.html A simple example Here, we give an example ...

  5. Graylog安装配置

    ES集群健康检测:curl -sXGET http://localhost:9200/_cluster/health?pretty=true | grep "status" | a ...

  6. 通过yumdownloader下载rpm包

    通过yum自带的一个工具:yumdownloader [root@web1 ~]#  rpm -qa |grep yum-utils [root@web1 ~]# yum -y install yum ...

  7. SPARK数据类型

    转自: http://www.cnblogs.com/tuitui1989/p/5331113.html 一.本地向量 有如下几个类: Vector(基类),DenseVector,SparseVec ...

  8. python 刷题必备

    1.判断输入的数字是否是回文数: 学习内容:把数字转成字符串 1. def is_palindrome(n): n=str(n) m=n[::-1] return n==m 2. tmp_str = ...

  9. hadoop发行版本

    Azure HDInsight Azure HDInsight is Microsoft's distribution of Hadoop. The Azure HDInsight ecosystem ...

  10. cdoj第13th校赛初赛A - AC Milan VS Juventus 【枚举】

    http://acm.uestc.edu.cn/#/contest/show/54 A - AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Oth ...