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 ...
随机推荐
- 通过OAuth2.0 获取授权访问SF 用户数据
站长资讯: 创建应用程序 新建应用程序 访问示例(Python+django) 环境准备: index.html 两种方式: 方式一:采用由用户授权,调用者无需知道SF的用户名与密码 方式二:直接 ...
- 数组,字符串方法总结 Unicode 数字
String.prototype.charCodeAt(index) 就是返回字符串中下标单个数值 对应的编码表的10进制表示数值 方法返回0到65535之间的整数,表示给定索引处的UTF-16代码 ...
- BZOJ4059[Cerc2012]Non-boring sequences(扫描线/分治)
这题正解应该是扫描线,就是发现DP的区间在两个维度都为连续段,于是可以直接扫描线.但不幸的是,扫描线常数过大,无法通过本题. 考虑分治.对于分治区间[l,r],可以记录pre和nxt表示其前/后一次出 ...
- 处理Ajax请求跨域问题
ajax跨域的原理 ajax出现请求跨域错误问题,主要原因就是因为浏览器的“同源策略”. CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resourc ...
- 怎么设置tomcat在get请求的中文也不乱码?两种情况下配置
我们都知道,get请求和post请求的一个比较显著区别就是,在使用post请求的时候,中文不会乱码,但是在使用get请求的时候,如果url后面带有中文就会乱码了.那么这个怎么解决呢? 前提:配置项目的 ...
- linux 上安装 tomcat
准备条件:安装java 一.tomcat 的安装 #新建文件夹 mkdir -p /data/tomcat #下载 tomcat8 服务器 wget http://mirrors.tuna.tsing ...
- [CTS2019]随机立方体(容斥+组合数学)
这题七次方做法显然,但由于我太菜了,想了一会发现也就只会这么多,而且别的毫无头绪.发现直接做不行,那么,容斥! f[i]为至少i个极值的方案,然后这里需要一些辅助变量,a[i]表示选出i个三维坐标均不 ...
- shell_切割日志
可以修改的:1.日志存放目录:logdir='/data/logs/'2.每个类型日志保留个数:savefiles=30 #!/bin/bashnum=$(date -d"+1 day ag ...
- mac环境下创建bash_profile文件并写入内容 更改php环境变量
1. 启动终端Terminal 2. 进入当前用户的home目录 输入cd ~ 3. 创建.bash_profile 输入touch .bash_profile 4. 编辑.bash_profile文 ...
- 吴裕雄--天生自然C语言开发:数据类型
#include <stdio.h> #include <limits.h> int main() { printf("int 存储大小 : %lu \n" ...