cf1051F. The Shortest Statement(最短路)
题意
题意:给出一张无向图,每次询问两点之间的最短路,满足$m - n <= 20$
$n, m, q \leqslant 10^5$
Sol
非常好的一道题。
首先建出一个dfs树。
因为边数-点数非常少,所以我们可以对于某些非树边特殊考虑。
具体做法是:对于非树边连接的两个点,暴力求出它们到所有点的最短路
对于询问的$(x, y)$
用树上的边,以及非树边连接的点到他们的最短路之和更新答案
由于边数的限制,非树边连接的点不会超过$2*(m - (n - 1)) = 42$个
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long
using namespace std;
const int MAXN = * 1e5 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M, Q, head[MAXN], num = , vis[MAXN], fa[MAXN][], dep[MAXN], happen[MAXN];
LL dis[][MAXN], Tdis[MAXN];
vector<int> p;
struct Edge {
LL u, v, w, f, nxt;
}E[MAXN];
inline void AddEdge(int x, int y, int z) {
E[num] = (Edge) {x, y, z, , head[x]};
head[x] = num++;
}
void dfs(int x, int _fa) {
vis[x] = ; dep[x] = dep[_fa] + ;
for(int i = head[x]; ~i; i = E[i].nxt) {
int to = E[i].v;
if(vis[to]) continue;
E[i].f = E[i ^ ].f = ;
Tdis[to] = Tdis[x] + (LL)E[i].w;
fa[to][] = x;
dfs(to, x);
}
}
void Dij(int x, int id) {
memset(dis[id], 0x7f, sizeof(dis[id])); dis[id][x] = ;
memset(vis, , sizeof(vis));
priority_queue<Pair> q; q.push(MP(, x));
while(!q.empty()) {
int p = q.top().se; q.pop();
if(vis[p]) continue;
for(int i = head[p]; ~i; i = E[i].nxt) {
int to = E[i].v;
if(dis[id][to] > dis[id][p] + E[i].w && (!vis[to]))
dis[id][to] = dis[id][p] + E[i].w, q.push(MP(-dis[id][to], to));
}
}
}
void Pre() {
for(int j = ; j <= ; j++)
for(int i = ; i <= N; i++)
fa[i][j] = fa[fa[i][j - ]][j - ];
}
int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = ; i >= ; i--)
if(dep[fa[x][i]] >= dep[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][];
}
main() {
// freopen("a.in", "r", stdin);
memset(head, -, sizeof(head));
N = read(); M = read();
for(int i = ; i <= M; i++) {
int x = read(), y = read(), z = read();
AddEdge(x, y, z);
AddEdge(y, x, z);
}
dfs(, );
for(int i = ; i < num; i++)
if(!E[i].f) {
if(!happen[E[i].u]) p.push_back(E[i].u), happen[E[i].u] = ;
if(!happen[E[i].v]) p.push_back(E[i].v), happen[E[i].v] = ;
} for(int i = ; i < p.size(); i++)
Dij(p[i], i);
Pre();
int Q = read();
while(Q--) {
int x = read(), y = read();
LL ans = Tdis[x] + Tdis[y] - * Tdis[lca(x, y)];
for(int i = ; i < p.size(); i++)
ans = min(ans, dis[i][x] + dis[i][y]);
cout << ans << endl;
}
return ;
}
cf1051F. The Shortest Statement(最短路)的更多相关文章
- 【题解】Luogu CF1051F The Shortest Statement
原题传送门:CF1051F The Shortest Statement 题目大意,给你一个稀疏图,q次查询,查询两点之间距离 边数减点小于等于20 这不是弱智题吗,23forever dalao又开 ...
- [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(最短路/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 ...
- Codeforces.1051F.The Shortest Statement(最短路Dijkstra)
题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...
- [CF1051F]The Shortest Statement
题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路 $n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$. 首先看到$ ...
- CF1051F The Shortest Statement Dijkstra + 性质分析
动态询问连通图任意两点间最短路,单次询问. 显然,肯定有一些巧妙地性质(不然你就发明了新的最短路算法了233)有一点很奇怪:边数最多只比点数多 $20$ 个,那么就可以将这个图看作是一个生成树,上面连 ...
- Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路
F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...
- codeforces 1051F The Shortest Statement
题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...
随机推荐
- Flask17 Flask_Script插件的使用
1 什么是Flask_Script 可以对flask应用做一些统一的操作 flask_script官网:点击前往 2 安装flask_script pip install -i https://pyp ...
- 用 R 画中国分省市地图
用 R 画中国分省市地图 (2010-11-18 16:25:34) 转载▼ 标签: 中国地图 营销 杂谈 分类: 数据分析 用R 也可以做出漂亮的依参数变化的中国地图. 主要参考(http://co ...
- Leetcode:206. Reverse Linked List
这题直接ac掉 class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; while(he ...
- 解决spring mybatis 整合后mapper接口注入失败
spring整合mybatis,在dao层我们只写一个接口,配置相应的*mapper.xml文件, 报如下错误: org.springframework.beans.factory.Unsatisfi ...
- 功防技术与实践第1.2章,kali初步了解
20169314 2016-2017-2 <网络攻防实践>/<网络攻击与防范>第2周学习总结 一.教材学习内容总结 1.hacker和cracker的区别 网络攻防分三部分:系 ...
- 开发过程中--使用base64解决传输字符串加密问题!
反正上周在解决开发公司小工具时,需要将用户输入的字符串加密处理传输,那就直接贴上代码吧,希望能帮上你们: strToArrayBufferToBase64(str) { let pos = 0, le ...
- JSONCPP开发环境搭建
环境设置 项目地址 https://github.com/open-source-parsers/jsoncpp.git 操作系统 64位 Fedora 24 安装jsoncpp $ git clon ...
- 2018CCPC网络赛A(优先队列,思维)
#include<bits/stdc++.h>using namespace std;priority_queue<pair<int,int>>q;int main ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- 洛谷P1065 作业调度方案
P1065 作业调度方案 题目描述 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作 ...