传送门

Luogu

解题思路

注意到 \(m - n \le 20\) ,所以这其实是一个树上问题,非树边至多只有21条,那么我们就可以暴力地对每一个非树边所连接的点求一次单源最短路,然后每次询问时,先访问两点的树上距离,再尝试用非树边更新答案,取最小值输出即可。

细节注意事项

  • 最短路不要写挂就好

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} typedef long long LL;
const int _ = 100010;
const int __ = 200010; int tot = 1, head[_], nxt[__], ver[__], w[__];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; } int n, m, q, f[22][_], dep[_];
int vis[_], mark[__], sign[_];
LL tdis[_], dis[50][_];
vector < int > vec; inline void dfs(int u) {
vis[u] = 1;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (vis[v]) continue;
f[0][v] = u, dep[v] = dep[u] + 1;
tdis[v] = tdis[u] + (LL) w[i];
mark[i] = mark[i ^ 1] = 1, dfs(v);
}
} inline void calc() {
for (rg int i = 1; i <= 20; ++i)
for (rg int j = 1; j <= n; ++j)
f[i][j] = f[i - 1][f[i - 1][j]];
} inline int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (rg int i = 20; ~i; --i)
if (dep[f[i][x]] >= dep[y]) x = f[i][x];
if (x == y) return x;
for (rg int i = 20; ~i; --i)
if (f[i][x] != f[i][y]) x = f[i][x], y = f[i][y];
return f[0][x];
} inline void Dijkstra(int s, int p) {
static priority_queue < pair < LL, int > > Q;
memset(vis, 0, sizeof vis);
memset(dis[p], 0x7f, sizeof dis[p]);
dis[p][s] = 0, Q.push(make_pair(0, s));
while (!Q.empty()) {
int u = Q.top().second; Q.pop();
if (vis[u]) continue; vis[u] = 1;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[p][v] > dis[p][u] + w[i])
dis[p][v] = dis[p][u] + w[i], Q.push(make_pair(-dis[p][v], v));
}
}
} inline LL solve(int x, int y) {
LL res = tdis[x] + tdis[y] - 2 * tdis[LCA(x, y)];
for (rg int i = 0; i < (int) vec.size(); ++i)
res = min(res, dis[i][x] + dis[i][y]);
return res;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int u, v, d, i = 1; i <= m; ++i)
read(u), read(v), read(d), Add_edge(u, v, d), Add_edge(v, u, d);
dfs(1), calc();
for (rg int i = 2; i <= tot; ++i)
if (!mark[i] && !sign[ver[i]])
vec.push_back(ver[i]), sign[ver[i]] = 1;
for (rg int i = 0; i < (int) vec.size(); ++i) Dijkstra(vec[i], i);
read(q); for (rg int x, y; q--; ) read(x), read(y), printf("%lld\n", solve(x, y));
return 0;
}

完结撒花 \(qwq\)

「CF1051F」The Shortest Statement的更多相关文章

  1. 题解 CF1051F 【The Shortest Statement】

    这道题思路比较有意思,第一次做完全没想到点子上... 看到题目第一反应是一道最短路裸题,但是数据范围1e5说明完全不可能. 这个时候可以观察到题目给出了一个很有意思的条件,就是说边最多比点多20. 这 ...

  2. 【题解】Luogu CF1051F The Shortest Statement

    原题传送门:CF1051F The Shortest Statement 题目大意,给你一个稀疏图,q次查询,查询两点之间距离 边数减点小于等于20 这不是弱智题吗,23forever dalao又开 ...

  3. 「译」forEach循环中你不知道的3件事

    前言 本文925字,阅读大约需要7分钟. 总括: forEach循环中你不知道的3件事. 原文地址:3 things you didn't know about the forEach loop in ...

  4. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  5. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  6. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  7. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  8. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  9. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

随机推荐

  1. 小tips:使用vue-cli脚手架搭建项目,关于eslint语法检测配置

    配置文件在项目根目录里,文件名以 .eslintrc.* 为名. 为了兼容以前写的代码,避免修改太多代码,把不符合自己习惯的规则去掉,简单配置代码: module.exports = { root: ...

  2. 同步选中所有checkbox

    $("input[type=checkbox][tag=ckAll]").change(function () $(this).parent().parent().siblings ...

  3. 【译】从 Rust 到不只是 Rust:PHP 语言领域

    From Rust to beyond: The PHP galaxy 译文 原文地址:https://mnt.io/2018/10/29/from-rust-to-beyond-the-php-ga ...

  4. 题解【SP2713】GSS4 - Can you answer these queries IV

    题目描述 You are given a sequence \(A\) of \(N(N \leq 100,000)\) positive integers. There sum will be le ...

  5. 结合sqlmap进行sql注入过程

    结合sqlmap进行sql注入:(-r后面是通过burp suite抓出来的请求包:-p后面是注入点,即该请求里携带的某个参数) Get请求的注入: ./sqlmap.py -r rss_test.t ...

  6. mysql yum 卸载取消开机自启动

    查询安装的yum源rpm -qa | grep -i mysql 使用yum remove卸载 yum remove 刚才过滤出来的包一个个来 剩下卸载不了使用 rpm -e --nodeps: rp ...

  7. Dataset数据的XML持久化处理

    主要方法是用ADO.NET的DataTale 填充到Dataset Dataset 内置了XML持久化的方法,WriteXML和ReadXML:简单的WinFrom实例:从数据库的表 private ...

  8. Python单元测试unittest与HTMLTestRunner报告生成

    本文为简单介绍,使用python自带模块unittest来进行单元测试 首先我们有一个需要测试的类,employee.py  定义了涨薪的方法.我们需要测试这个类的功能是否正确. class Empl ...

  9. 概率dp poj 3071

    题目首先给出一个n,表示比赛一共进行n轮,那么队伍就有2^n只队伍输入一个2^n*2^n的矩阵,p[i][j]代表队伍i打败队伍j的概率dp[i][j]代表第i轮比赛的时候,队伍j赢的概率首先初始化时 ...

  10. [洛谷P4463] calc (生成函数)

    首先注意到题目中 \(a\) 数组是有序的,那我们只用算有序的方案乘上 \(n!\) 即可. 而此时的答案显然 \[Ans=[x^n](1+x)(1+2x)\dots (1+Ax)=\prod_{i= ...