[CF1051F]The Shortest Statement
题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路
$n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$.
首先看到$m$-$n\le20$这条限制,我们可以想到是围绕这个20来做这道题。
即如果我们随便在图上找一棵树,有最多21条非树边,连接最多42个顶点
考虑两点$x,y$之间的最短路就是某个点到$x$和$y$的最短路之和
首先对于只走树边的情况,这个点是两点的$LCA$
如果经过非树边,$x$或$y$到枚举的这个点的最短路上的最后一条边一定是非树边(如果都是树边的话完全可以转化到一个连接着非树边的点上去)
然后对于所有连接非树边的点都跑一遍最短路,询问时直接枚举这些点取$min$即可
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define M 200010
#define int long long
using namespace std;
int read()
{
char ch=getchar();int x=;
while(ch>''||ch<'') ch=getchar();
while(ch<=''&&ch>='') x=x*+ch-'',ch=getchar();
return x;
}
struct point{
int to,next,dis;
}e[M<<];
int n,m,num,Q,top;
int q[M],head[M],deep[M],dist[M];
int dis[][M],fa[M][];
bool vis[M];
struct node{int id,dis;};
bool operator < (node a1,node a2) {return a1.dis>a2.dis;}
void add(int from,int to,int dis)
{
e[++num].next=head[from];
e[num].to=to;
e[num].dis=dis;
head[from]=num;
}
void dfs(int x,int f)
{
vis[x]=true; fa[x][]=f;
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(to==f) continue;
if(vis[to]) q[++top]=x,q[++top]=to;
else
{
deep[to]=deep[x]+;
dist[to]=dist[x]+e[i].dis;
dfs(to,x);
}
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
for(int i=;i>=;i--)
if(deep[fa[x][i]]>=deep[y])
x=fa[x][i];
if(x==y) return x;
for(int i=;i>=;i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][];
}
void Dijkstra(int id)
{
memset(dis[id],,sizeof(dis[id]));
memset(vis,false,sizeof(vis));
priority_queue<node>Q;
dis[id][q[id]]=;
Q.push((node){q[id],});
while(!Q.empty())
{
int x=Q.top().id;Q.pop();
if(vis[x]) continue;
vis[x]=true;
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(!vis[to]&&dis[id][x]+e[i].dis<dis[id][to])
{
dis[id][to]=dis[id][x]+e[i].dis;
Q.push((node){to,dis[id][to]});
}
}
}
}
#undef int
int main()
{
#define int long long
n=read(); m=read();
for(int i=;i<=m;i++)
{
int a=read(),b=read(),c=read();
add(a,b,c); add(b,a,c);
}
deep[]=;dfs(,);
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
sort(q+,q++top); top=unique(q+,q++top)-q-;
for(int i=;i<=top;i++) Dijkstra(i);
Q=read();
while(Q--)
{
int x=read(),y=read();
int ans=dist[x]+dist[y]-*dist[lca(x,y)];
for(int i=;i<=top;i++) ans=min(ans,dis[i][x]+dis[i][y]);
printf("%lld\n",ans);
}
return ;
}
[CF1051F]The Shortest Statement的更多相关文章
- 【题解】Luogu CF1051F The Shortest Statement
原题传送门:CF1051F The Shortest Statement 题目大意,给你一个稀疏图,q次查询,查询两点之间距离 边数减点小于等于20 这不是弱智题吗,23forever dalao又开 ...
- cf1051F. The Shortest Statement(最短路/dfs树)
You are given a weighed undirected connected graph, consisting of nn vertices and mm edges. You shou ...
- CF1051F The Shortest Statement 题解
题目 You are given a weighed undirected connected graph, consisting of n vertices and m edges. You sho ...
- [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)
题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...
- cf1051F. The Shortest Statement(最短路)
题意 题目链接 题意:给出一张无向图,每次询问两点之间的最短路,满足$m - n <= 20$ $n, m, q \leqslant 10^5$ Sol 非常好的一道题. 首先建出一个dfs树. ...
- CF1051F The Shortest Statement Dijkstra + 性质分析
动态询问连通图任意两点间最短路,单次询问. 显然,肯定有一些巧妙地性质(不然你就发明了新的最短路算法了233)有一点很奇怪:边数最多只比点数多 $20$ 个,那么就可以将这个图看作是一个生成树,上面连 ...
- codeforces 1051F The Shortest Statement
题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...
- The Shortest Statement CodeForces - 1051F(待测试)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
- Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路
F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...
随机推荐
- quartz任务调度配置 解决jobDetail身份标识存在问题
applicationContext.xml配置:每五秒轮询一次.注意:周和日期不能同时为通配符”*“ <bean id="planInfoServiceImpl" clas ...
- java项目规范
一.命名规范 1. 项目名全部小写 2. 包名全部小写 3. 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. 如:public class MyFirstClass{} 4. 变量 ...
- 20165330 2017-2018-2 《Java程序设计》第5周学习总结
课本知识总结 第七章 内部类与异常类 内部类:类的一种成员 外嵌类:包含内部类的类称为内部类的外嵌类 二者关系: 内部类的外嵌类的成员变量在内部类中仍然有效,内部类中的方法也可以调用外嵌类中的方法. ...
- hdu2254 奥运 矩阵的应用
hdu2254 奥运 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2254 题意:题目让我们求得是的可以得到的金牌数量,而和金牌数量=在t1到t2天( ...
- .c和.h的联系
.c文件就是C语言系列的源文件,而H文件则是C语言的头文件,即C系列中存放函数和全局变量的文件,因为C中的函数是被封装起来的,即无法看到其代码. 子程序不要定义在*.h中.函数定义要放在*.c中,而* ...
- Spark Streaming源码分析 – DStream
A Discretized Stream (DStream), the basic abstraction in Spark Streaming, is a continuous sequence o ...
- HDFS集群启动start-dfs.sh报错
[root@master sbin]# start-dfs.sh Starting namenodes on [master] master: Error: JAVA_HOME is not set ...
- Gunner II--hdu5233(map&vector/二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...
- Python慢,为啥还有大公司用?
PyCon 是全世界最大的以 Python 编程语言 为主题的技术大会,大会由 Python 社区组织,每年举办一次.在 Python 2017 上,Instagram 的工程师们带来了一个有关 Py ...
- Java集合—Queue(转载)
Queue用于模拟队列这种数据结构,队列通常是指“先进先出”(FIFO)的容器.新元素插入(offer)到队列的尾部,访问元素(poll)操作会返回队列头部的元素.通常,队列不允许随机访问队列中的元素 ...