「CF1051F」The Shortest Statement
传送门
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的更多相关文章
- 题解 CF1051F 【The Shortest Statement】
这道题思路比较有意思,第一次做完全没想到点子上... 看到题目第一反应是一道最短路裸题,但是数据范围1e5说明完全不可能. 这个时候可以观察到题目给出了一个很有意思的条件,就是说边最多比点多20. 这 ...
- 【题解】Luogu CF1051F The Shortest Statement
原题传送门:CF1051F The Shortest Statement 题目大意,给你一个稀疏图,q次查询,查询两点之间距离 边数减点小于等于20 这不是弱智题吗,23forever dalao又开 ...
- 「译」forEach循环中你不知道的3件事
前言 本文925字,阅读大约需要7分钟. 总括: forEach循环中你不知道的3件事. 原文地址:3 things you didn't know about the forEach loop in ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
随机推荐
- QT离线安装包
各个版本下载链接http://mirrors.ustc.edu.cn/qtproject/archive/qt http://www.qtcn.org/bbs/i.php
- anniversary party_hdu1520
本来以为是一道很简单的提,可以分分钟解决(实际上就是很简单) 然而一直报错,找半天,竟然要多组输入(还是太菜了) 所以每组需要先初始化, 这是一道树形DP的简单题,具体思路就是我选这个上司就不能选他的 ...
- BigInteger和BigDecimal的基本用法
整型大数 BigInteger: import java.math.BigInteger; import java.util.Scanner; public class Main { public s ...
- 在UTF-8页面中引入编码为GBK的JavaScript文件乱码问题了
原文地址:http://js8.in/2009/12/11/%E5%AF%B9%E5%BC%95%E7%94%A8%E5%A4%96%E9%83%A8javascript%E9%A1%B5%E9%9D ...
- 第三十四篇 玩转数据结构——哈希表(HashTable)
1.. 整型哈希函数的设计 小范围正整数直接使用 小范围负整数整体进行偏移 大整数,通常做法是"模一个素数" 2.. 浮点型哈希函数的设计 转成整型进行处理 3.. 字符串 ...
- Django框架中auth模块的详解
auth模块 auth模块是对登录认证方法的一种封装,本身就是一个对象,可以获取用户的详细信息,有了auth模块可以验证登录信息是否存在数据库中,还可以检验用户是否已经登录,校验密码等 auth方法 ...
- GO111MODULE
GO111MODULE的功能: //为了解决项目不用放在go的src目录下,src应该放一些官方的包,而不应该放项目 //模块是相关Go包的集合.modules是源代码交换和版本控制的单元. go命令 ...
- Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览
一.设计原理 1.Hadoop架构: 流水线(PipeLine) 2.Hadoop架构: HDFS中数据块的状态及其切换过程,GS与BGS 3.Hadoop架构: 关于Recovery (Lease ...
- python中pip问题
1.在cmd中运行pip命令显示‘pip命令显示不是内部或外部命令,也不是可运行的程序或批处理文件’的问题 先看python的安装目录下Script文件夹中pip3.exe有没有缺失 如果没有在cmd ...
- 【StarUML】时序图
时序图是可视化地展示对象与对象之间的联系的图,与其他的图相比,它跟侧重于表现为了完成一个用例,对象之间是怎么协同工作的. 之前学习的组件图.用例图都能表现对象之间的联系,侧重的是"有哪些联系 ...