题目链接

传送门

题面



题意

给你一张有\(n\)个点\(m\)条边的联通图(其中\(m\leq n+20)\),\(q\)次查询,每次询问\(u\)与\(v\)之间的最短路。

思路

由于边数最多只比点数大21,因此我们可以先跑出一棵最小生成树,然后将非树上边的两个端点跑一边最短路,然后每次查询就比较\(max((dis[u]+dis[v]-2dis[lca(u,v)]),dist[i][u]+dis[i][v])\),其中\(dis[u]\)表示\(u\)到最小生成树根节点的距离,\(dist[i][u]\)表示第\(i\)个点到\(u\)的最短路,其中\(i\)为非树边的某个端点。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> piL;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("in","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, m, tot, u, v, q;
set<int> s;
set<int>::iterator it;
int fa[maxn], vis[maxn], head[maxn];
LL dis[45][maxn]; struct node {
int u, v, w;
bool operator < (const node& x) const {
return w < x.w;
}
}pp[maxn]; struct edge {
int v, w, next;
}ed[maxn<<1]; void init() {
tot = 0;
for(int i = 0; i <= n; i++) {
head[i] = -1;
fa[i] = i;
}
} void add(int u, int v, int w) {
ed[tot].v = v;
ed[tot].w = w;
ed[tot].next = head[u];
head[u] = tot++;
} struct LCA {
int tot;
int deep[maxn], fa[maxn][30], head[maxn];
LL cost[maxn];
struct edge {
int v, w, next;
}ed[maxn<<1]; void init() {
tot = 0;
for(int i = 0; i <= n; i++) {
head[i] = -1;
cost[i] = deep[i] = 0;
}
} void add(int u, int v, int w) {
ed[tot].v = v;
ed[tot].w = w;
ed[tot].next = head[u];
head[u] = tot++;
} void dfs(int u, int d, int p) {
deep[u] = d, fa[u][0] = p;
for(int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if(v == p) continue;
cost[v] = cost[u] + ed[i].w;
dfs(v, d + 1, u);
}
} void lca() {
for(int i = 1; i <= n; i++) {
for(int j = 1; (1<<j) <= n; j++) {
fa[i][j] = -1;
}
}
for(int j = 1; (1<<j) <= n; j++) {
for(int i = 1; i <= n; i++) {
if(fa[i][j-1] != -1) {
fa[i][j] = fa[fa[i][j-1]][j-1];
}
}
}
} int query(int u, int v) {
if(deep[u] <= deep[v]) swap(u, v);
int k;
for(k = 0; (1 << (1 + k)) <= deep[u]; k++);
for(int i = k; i >= 0; i--) {
if(deep[u] - (1<<i) >= deep[v]) {
u = fa[u][i];
}
}
if(u == v) return u;
for(int i = k; i >= 0; i--) {
if(fa[u][i] != -1 && fa[u][i] != fa[v][i]) {
u = fa[u][i], v = fa[v][i];
}
}
return fa[u][0];
} LL dis(int u, int v) {
return cost[u] + cost[v] - 2 * cost[query(u, v)];
}
}L; void dij(int s, int num) {
for(int i = 1; i <= n; ++i) {
dis[num][i] = INF, vis[i] = 0;
}
priority_queue<pLi, vector<pLi>, greater<pLi> > q;
dis[num][s] = 0;
q.push({0, s});
int u, v;
while(!q.empty()) {
u = q.top().second; q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; ~i; i = ed[i].next) {
v = ed[i].v;
if(dis[num][v] > dis[num][u] + ed[i].w) {
dis[num][v] = dis[num][u] + ed[i].w;
q.push({dis[num][v], v});
}
}
}
} int fi(int x) {
return fa[x] == x ? x : fa[x] = fi(fa[x]);
} void kruskal() {
sort(pp + 1, pp + m + 1);
for(int i = 1; i <= m; ++i) {
int p1 = fi(pp[i].u), p2 = fi(pp[i].v);
if(p1 == p2) continue;
fa[p1] = p2;
vis[i] = 1;
}
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d", &pp[i].u, &pp[i].v, &pp[i].w);
}
init();
kruskal();
L.init();
for(int i = 1; i <= m; ++i) {
if(vis[i]) L.add(pp[i].u, pp[i].v, pp[i].w), L.add(pp[i].v, pp[i].u, pp[i].w);
else s.insert(pp[i].u), s.insert(pp[i].v);
add(pp[i].u, pp[i].v, pp[i].w), add(pp[i].v, pp[i].u, pp[i].w);
}
L.dfs(1, 0, 0);
L.lca();
int num = 0;
for(it = s.begin(); it != s.end(); ++it) dij(*it, num++);
scanf("%d", &q);
while(q--) {
scanf("%d%d", &u, &v);
LL ans = L.dis(u, v);
for(int i = 0; i < num; ++i) {
ans = min(ans, dis[i][u] + dis[i][v]);
}
printf("%lld\n", ans);
}
return 0;
}

The Shortest Statement(Educational Codeforces Round 51 (Rated for Div.2)+最短路+LCA+最小生成树)的更多相关文章

  1. Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路

    F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...

  2. Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement

    题目链接:The Shortest Statement 今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路.看来这种题还挺常见的. 为什么最终答案要从42个点的最短路(到$x,y$) ...

  3. Educational Codeforces Round 51 (Rated for Div. 2)

    做了四个题.. A. Vasya And Password 直接特判即可,,为啥泥萌都说难写,,,, 这个子串实际上是忽悠人的,因为每次改一个字符就可以 我靠我居然被hack了???? %……& ...

  4. CodeForces Educational Codeforces Round 51 (Rated for Div. 2)

    A:Vasya And Password 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen(&q ...

  5. 【 Educational Codeforces Round 51 (Rated for Div. 2) F】The Shortest Statement

    [链接] 我是链接,点我呀:) [题意] [题解] 先处理出来任意一棵树. 然后把不是树上的边处理出来 对于每一条非树边的点(最多21*2个点) 在原图上,做dijkstra 这样就能处理出来这些非树 ...

  6. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  9. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

随机推荐

  1. FinalShell—一体化服务器管理软件(SSH客户端)

    下面附上一些截图和官方连接: 官网:http://www.hostbuf.com/ FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发 ...

  2. 如何在Debian 9上安装和使用Docker

    介绍 Docker是一个简化容器中应用程序进程管理过程的应用程序.容器允许您在资源隔离的进程中运行应用程序.它们与虚拟机类似,但容器更便携,更加资源友好,并且更依赖于主机操作系统. 在本教程中,您将在 ...

  3. jq同一页面内容切换

    $(function() { //选择标题显示 初始显示内容及样式 $('.right-content .right-item').eq(0).addClass('showcontent') $('. ...

  4. [转] 浅谈 OpenResty

    一.前言 我们都知道Nginx有很多的特性和好处,但是在Nginx上开发成了一个难题,Nginx模块需要用C开发,而且必须符合一系列复杂的规则,最重要的用C开发模块必须要熟悉Nginx的源代码,使得开 ...

  5. Navicat 创建oracle表空间、新建用户、授权

    1.利用数据库管理员账号:SYSTEM,再配合数据库管理口令,连接Oracle数据库. 登录界面: 2.创建表空间文件 进入如下界面 进入如下界面 弹出如下界面,输入表空间名称 最终结果:  2 .新 ...

  6. [终极巨坑]golang+vue开发日记【二】,登陆界面制作(一)

    写在前面 本期内容是适合第一次使用vue或者golang开发的,内容会以实战的形式来讲解.看懂本段内容需要了解基础内容有html,css,最好可以看一下vue的基础.并且这里的每个知识点不可能详细解说 ...

  7. 洛谷 P1411 树 (树形dp)

    大意: 给定树, 求删除一些边, 使得连通块大小的乘积最大 设$dp_{i,j}$表示只考虑点$i$的子树, $i$所在连通块大小为$j$的最大值. 转移的时候不计算$i$所在连通块的贡献, 留到最后 ...

  8. 回文树/回文自动机(PAM)学习笔记

    回文树(也就是回文自动机)实际上是奇偶两棵树,每一个节点代表一个本质不同的回文子串(一棵树上的串长度全部是奇数,另一棵全部是偶数),原串中每一个本质不同的回文子串都在树上出现一次且仅一次. 一个节点的 ...

  9. word表格中怎么添加递增的序号

    word2013表格中怎么添加递增的序号?word2013表格中想要让第一类自动显示递增序号,该怎么操作呢?下面我们就来分享两种方法,需要的朋友可以参考下 工具/原料   word2013 通过项目编 ...

  10. .NET 的程序集加载上下文

    原文:.NET 的程序集加载上下文 我们编写的 .NET 应用程序会使用到各种各样的依赖库.我们都知道 CLR 会在一些路径下帮助我们程序找到依赖,但如果我们需要手动控制程序集加载路径的话,需要了解程 ...