传送门:

这是一道bzoj权限题

Luogu团队题链接

解题思路

首先对于每一个点 \(x\) 预处理出 \(nr[x]\) 和 \(dis[x]\),分别表示离 \(x\) 最近的加油站以及该段距离。

这个过程可以用多源 \(\text{Dijkstra}\) 处理。

然后对于每一条原图中的边 \((u, v, w)\)(\(nr[u]\ne nr[v]\))

改为这样一条边:\((nr[u], nr[v], dis[u] + dis[v] + w)\)

然后只要离线用并查集维护一下连通性即可。

细节注意事项

  • 最短路不要写挂啊

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
#define pii pair < int, int >
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;
} const int _ = 200010;
const int __ = 400010; int tot, 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 q, n, m, k, c[_], vis[_], dis[_], nr[_];
struct node{ int u, v, d, id; }ask[_], g[__], e[__]; int ans[_]; inline bool cmp(const node& x, const node& y) { return x.d < y.d; } inline void Dijkstra() {
static priority_queue < pii > Q;
memset(dis, 0x3f, sizeof dis);
for (rg int i = 1; i <= k; ++i)
Q.push(make_pair(dis[c[i]] = 0, c[i])), nr[c[i]] = c[i];
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[v] > dis[u] + w[i])
dis[v] = dis[u] + w[i], nr[v] = nr[u], Q.push(make_pair(-dis[v], v));
}
}
} int fa[_]; inline int findd(int x) { return fa[x] == x ? x : fa[x] = findd(fa[x]); } inline void merge(int x, int y) { fa[findd(x)] = findd(y); } int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(k), read(m);
for (rg int i = 1; i <= k; ++i) read(c[i]);
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), g[i] = (node) { u, v, d };
Dijkstra();
int cnt = 0;
for (rg int u, v, i = 1; i <= m; ++i) {
u = g[i].u, v = g[i].v;
if (nr[u] != nr[v]) e[++cnt] = (node) { nr[u], nr[v], dis[u] + dis[v] + g[i].d };
}
read(q);
for (rg int i = 1; i <= q; ++i)
read(ask[i].u), read(ask[i].v), read(ask[i].d), ask[i].id = i;
sort(ask + 1, ask + q + 1, cmp);
sort(e + 1, e + cnt + 1, cmp);
for (rg int i = 1; i <= n; ++i) fa[i] = i;
for (rg int i = 1, j = 1; i <= q; ++i) {
while (j <= cnt && e[j].d <= ask[i].d) merge(e[j].u, e[j].v), ++j;
ans[ask[i].id] = findd(ask[i].u) == findd(ask[i].v);
}
for (rg int i = 1; i <= q; ++i) puts(ans[i] ? "TAK" : "NIE");
return 0;
}

完结撒花 \(qwq\)

「AMPPZ2014」Petrol的更多相关文章

  1. [题解] [BZOJ4144] 「AMPPZ2014」Petrol

    题面 怎么是权限题啊 题解 有一次考过, 但是不记得了 如果每个点都是加油站的话, 这道题就是货车运输 考虑如何转化 我们可以设

  2. 题解【BZOJ4145】「AMPPZ2014」The Prices

    题目描述 你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\),在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i] ...

  3. 「AMPPZ2014」The Captain

    传送门: 这是一道bzoj权限题 Luogu团队题链接 解题思路 直接连边的话边数肯定会爆炸,考虑减少边数. 我们画出坐标系,发现一个东西: 对于两个点 \(A,B\),\(|x_A-y_A|\) 可 ...

  4. 「AMPPZ2014」The Prices

    传送门 Luogu团队题链接 解题思路 看到 \(m\) 这么小,马上想到状压 \(\text{DP}\). 设 \(dp[i][j]\) 表示在前 \(i\) 家商店中已买商品的状态为 \(j\) ...

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

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

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

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

  7. JavaScript OOP 之「创建对象」

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

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

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

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

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

随机推荐

  1. Echarts字体和线条颜色设置操作笔记

    在网上偶然看到的一篇文章 感觉不错  下面是原著地址 原著地址:https://blog.csdn.net/eastmount/article/details/52823548

  2. Bug搬运工-CSCvf74866:Webauth scaling improvements - fix problem with GUI going unresponsive with HTTPS redirect

    Webauth scaling improvements - fix problem with GUI going unresponsive with HTTPS redirect CSCvf7486 ...

  3. provide 和 inject高阶使用

    provide 在祖先里授权导出 inject在后代负责接收 foo可以是本组件的函数方法 或者 变量foo 也可以是祖先组件自己 祖先组件foo: this 后代组件 foo.$options.da ...

  4. Atcoder Beginner Contest145E(01背包记录路径)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[3007],b[3007];int ...

  5. 猜解数据库(MYSQL)信息

    /Less-1/?id=1' and if (length(database())=8,sleep(5),0) --+ 注:http://43.247.91.228:84/Less-1/为靶场地址,发 ...

  6. 第八届极客大挑战 Re

    0x01.Writeup-RE-CM_2 题目: 解题思路: 1.这个是经过xor的,王老师提示说用xortool,于是放进kali,装好之后执行 xortool CM_2.exe -b, 0.out ...

  7. 05-Docker-Container资源限制

    目录 05-Docker-Container资源限制 参考 可压缩性 MEM限制 选项说明 压测示例 CPU限制 选项说明 压测示例 05-Docker-Container资源限制 Docker Ve ...

  8. ASP.NET Core搭建多层网站架构【10-使用JWT进行授权验证】

    2020/01/31, ASP.NET Core 3.1, VS2019, Microsoft.AspNetCore.Authentication.JwtBearer 3.1.1 摘要:基于ASP.N ...

  9. 【兆易创新RISC-V开发板评测】01.干货分享

    背景介绍:2019年12月19日在面板包偶然发可以免费申请测评GD32VF103开发板,欣喜万分:在这之前各大技术论坛说是已经有国产兆易创新的RISCV指令集的MCU发布的事情,一时间摩拳擦掌想购入一 ...

  10. VBA 学习笔记 - 变量与常量

    学习资料:https://www.yiibai.com/vba/vba_variables.html 变量和常量命名规则 必须以字母开头 不能包含空格.句点(.).感叹号(!)或字符@,&,$ ...