CF_Edu.#51_Div.2_1051F_The Shortest Statement
F. The Shortest Statement
time limit per test:4 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
You are given a weighed undirected connected graph, consisting of \(n\) vertices and \(m\) edges.
You should answer \(q\) queries, the i-th query is to find the shortest distance between vertices \(u_i\) and \(v_i\).
给定n个点,m条边,q个询问,求每次询问的两点间的最短路。
Input
The first line contains two integers \(n\) and \(m (1≤n,m≤10^5,m−n≤20)\) — the number of vertices and edges in the graph.
Next \(m\) lines contain the edges: the i-th edge is a triple of integers \(v_i,u_i,d_i (1≤u_i,v_i≤n,1≤d_i≤10^9,u_i≠v_i)\). This triple means that there is an edge between vertices ui and vi of weight \(d_i\). It is guaranteed that graph contains no self-loops and multiple edges.
The next line contains a single integer \(q (1≤q≤10^5)\) — the number of queries.
Each of the next \(q\) lines contains two integers \(u_i\) and \(v_i (1≤u_i,v_i≤n)\) — descriptions of the queries.
Pay attention to the restriction \(m−n ≤ 20\).
Output
Print\(q\)lines.
The i-th line should contain the answer to the i-th query — the shortest distance between vertices \(u_i\) and \(v_i\).
Examples
Input
3 3
1 2 3
2 3 1
3 1 5
3
1 2
1 3
2 3
Output
3
4
1
Input
8 13
1 2 4
2 3 6
3 4 1
4 5 12
5 6 3
6 7 8
7 8 7
1 4 1
1 8 3
2 6 9
2 7 1
4 6 3
6 8 2
8
1 5
1 7
2 3
2 8
3 7
3 4
6 8
7 8
Output
7
5
6
7
7
1
2
7
SOLUTION
本题一开始说是要求\(n=10^5\)的多源最短路我被吓到了,裸做单源最短路是不可能的,就又看到了边数的限制:\(m-n\leq 20\)。
这就意味着,在n,m很大的绝大多数情况下,20并不能造成很大的影响。
所以换而言之,这题的模型可以近似地看作是一棵树。因为对于绝大多数的点来说,它们要走的最短路径的确全是树上路径。
树上最短路?求LCA啊。这样我们就可以解决绝大多数的点。
不过那20条边的确不能忽视,因为可能存在更优解要经过那21条边中的一些。而那21条边影响的是什么呢?当然是每条边的两个端点分别关于其他点的最短路啊(存这种端点的时候一定记得要去重!!!)。因为本题的余边只有21条,就可以考虑暴力做至多42遍dijkstra。。。得到了关于以编号为\(j\)的点为起点,图上点\(i\)的单源最短路数组\(dp[i][j]\)。
所以到了最后,我们可以得出,在本题内,最短路的答案要么只从树上的LCA算得,要么就可能走那多余的21条边。
这题是根据数据性质来猜正解的好题。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
#define Min(a,b) ((a<b)?a:b)
#define Max(a,b) ((a>b)?a:b)
const int N=101000,MN=45;
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
return x*f;}
struct EDGE{int nxt,to,w;}e[2*N];
int n,m,Q,low[N],vis[N],fa[N][20],head[N],dpt[N],nd[MN],used[N],cnt=0,cnt2=0;
LL dist[N][MN],dist2[N];
inline void add(int u,int v,int w){e[++cnt].nxt=head[u];e[cnt].to=v;e[cnt].w=w;head[u]=cnt;}
void dfs(int u,int fath){
used[u]=1;dpt[u]=dpt[fath]+1;fa[u][0]=fath;
for (int i=1;i<=low[dpt[u]];++i){fa[u][i]=fa[fa[u][i-1]][i-1];}
for (int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=e[i].w;
if (v==fath) continue;
if (!used[v]) {dist2[v]=dist2[u]+w;dfs(v,u);}
else{if (used[u]==1) nd[++cnt2]=u,used[u]++;
if (used[v]==1) nd[++cnt2]=v,used[v]++;}
}
}
struct NODE{LL d;int u;bool operator< (const NODE &a)const{return d>a.d;}};
void dij(int stt,int now){
for (int i=1;i<=n;++i) dist[i][now]=1e18+7;
priority_queue<NODE> q;q.push((NODE){0,stt});dist[stt][now]=0;
memset(vis,0,sizeof(vis));
while (!q.empty()){
NODE ntp=q.top();q.pop();
int u=ntp.u;
if (vis[u]) continue;
vis[u]=1;
for (int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=e[i].w;
if (dist[v][now]>dist[u][now]+w){
dist[v][now]=dist[u][now]+w;
q.push((NODE){dist[v][now],v});
}
}
}
}
inline int lca(int x,int y){
if (dpt[x]<dpt[y]) swap(x,y);
while (dpt[x]>dpt[y]) {x=fa[x][low[dpt[x]-dpt[y]]];}
if (x==y) return x;
for (int i=low[dpt[x]];i>=0;--i)
if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];}
int main(){
int i,j;
n=read();m=read();memset(head,0,sizeof(head));
memset(used,0,sizeof(used));memset(dist2,0,sizeof(dist2));
for (i=1;i<=m;++i){
int u=read(),v=read(),w=read();add(u,v,w);add(v,u,w);}
low[1]=0;for (i=2;i<=n;++i) low[i]=low[i>>1]+1;
dpt[0]=0;dfs(1,0);
// sort(nd+1,nd+1+cnt2);cnt2=unique(nd+1,nd+1+cnt2)-nd-1;
for (i=1;i<=cnt2;++i) {int stt=nd[i];dij(stt,i);}
Q=read();
for (i=1;i<=Q;++i){
int u=read(),v=read();
int ast=lca(u,v);
LL DIST=dist2[u]+dist2[v]-dist2[ast]*2;
for (j=1;j<=cnt2;++j) DIST=Min(DIST,dist[u][j]+dist[v][j]);
printf("%I64d\n",DIST);
}
return 0;
}
CF_Edu.#51_Div.2_1051F_The Shortest Statement的更多相关文章
- 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> ...
- 【题解】Luogu CF1051F The Shortest Statement
原题传送门:CF1051F The Shortest Statement 题目大意,给你一个稀疏图,q次查询,查询两点之间距离 边数减点小于等于20 这不是弱智题吗,23forever dalao又开 ...
- Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路
F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...
- Codeforces 1051E Vasya and Big Integers&1051F The Shortest Statement
1051E. Vasya and Big Integers 题意 给出三个大整数\(a,l,r\),定义\(a\)的一种合法的拆分为把\(a\)表示成若干个字符串首位相连,且每个字符串的大小在\(l, ...
- CF 1051 F. The Shortest Statement
F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...
- Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement
题目链接:The Shortest Statement 今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路.看来这种题还挺常见的. 为什么最终答案要从42个点的最短路(到$x,y$) ...
- cf1051F. The Shortest Statement(最短路/dfs树)
You are given a weighed undirected connected graph, consisting of nn vertices and mm edges. You shou ...
- [CF1051F] Shortest Statement
问题描述 You are given a weighed undirected connected graph, consisting of n vertices and m edges. You s ...
随机推荐
- 思考题:clock类编写
题目 为便于后文理解,这里先补上这份代码前文开的库以及宏定义: #include<cstdio> #include<iostream> #include<string&g ...
- cisco3900板卡sm-es3g-24-p使用方法
不知道是不是叫板卡,还是叫线卡希望不予深究.本文摘自:https://zhidao.baidu.com/question/1669814353056144947.html 插上板卡后,在配置界面仅显示 ...
- TF分布式问题
碰到一个没解决的问题. 用tensorflow 分布式异步更新模式训练模型, 模型中带正则项, 每个batch的损失函数为 \[\lambda \|W\|_1 + \frac 1 {N_j} \sum ...
- UML-FURPS+与补充性规格说明
1.FURPS+ 在统一过程(UP)中,需求按照“FURPS+”模型进行分类. 功能性(Functional):特性.功能.安全性: 可用性(Usability):人性化因素.帮助.文档: 可靠性(R ...
- java的io字符流关闭和刷新.flush();
因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中. 但是关闭的流对象,是无法继续写出数据 的.如果我们既想写出数据,又想继续使用流,就需要 flush 方法了. flush :刷新缓冲区, ...
- Django-rest framework框架的三大认证组件
源码分析:三大认证组件的封装 组件的认证配置: 模型层:models.py class User(BaseModel): username = models.CharField(verbose_nam ...
- nginx反代及后端web配置
一.反代配置,proxy_pass指向的upstream需要在反代的nginx.conf中配置 server {access_log /home/nginx/front_access.log;erro ...
- SLAM资料
当下SLAM方案的总体介绍 http://wwwbuild.net/roboteasy/908066.html slam基础知识 https://www.zhihu.com/question/3518 ...
- 【iOS学习笔记】UITextField中的输入检测——限制只能输入数字和小数点
最近趁着放假时间,在看The Big Nerd Ranch的iOS编程,想着重新复习一遍iOS开发的基础知识 于是从这一篇开始记录一些学习过程中遇到的小问题 书中第四章有一个温度转换的app实现,整体 ...
- HDU-4004 The Frog's Games (分治)
http://acm.hdu.edu.cn/showproblem.php?pid=4004 Problem Description The annual Games in frogs' kingdo ...