多源最短路+并查集

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
typedef pair<int, int> P;
const int N = 2e5 + ;
const int inf = 2e9 + ;
vector<P> g[N];
int c[N], d[N], closest[N];
struct Query {
int x, y, b, ans, id;
void read() {
scanf("%d%d%d", &x, &y, &b);
}
}b[N];
struct triple {
int u, v, w;
};
int fa[N];
int findx(int x) {
if (x == fa[x]) return x;
return fa[x] = findx(fa[x]);
}
int main() {
int n, m, s;
scanf("%d%d%d", &n, &s, &m);
rep(i, , s) scanf("%d", &c[i]);
vector<triple> e;
rep(i, , m) {
int u, v, d;
scanf("%d%d%d", &u, &v, &d);
g[u].push_back(P(v, d));
g[v].push_back(P(u, d));
e.push_back((triple){u, v, d});
}
auto dijkstra = [&]() {
priority_queue<P, vector<P>, greater<P> > Q;
rep(i, , n) d[i] = inf;
rep(i, , s) {
d[c[i]] = ; closest[c[i]] = c[i];
Q.push(P(, c[i]));
}
while (!Q.empty()) {
P r = Q.top(); Q.pop();
int u = r.second;
if (d[u] < r.first) continue;
for (P p: g[u]) {
int v = p.first;
if (d[v] > d[u] + p.second) {
d[v] = d[u] + p.second;
closest[v] = closest[u];
Q.push(P(d[v], v));
}
}
}
};
dijkstra();
int q;
scanf("%d", &q);
rep(i, , q) b[i].read(), b[i].id = i;
auto calc = [&](triple &x) -> int{
return d[x.u] + d[x.v] + x.w;
};
sort(b + , b + + q, [&](Query &a, Query &b) {
return a.b < b.b;
});
sort(begin(e), end(e), [&](triple &a, triple &b) {
return calc(a) < calc(b);
});
rep(i, , n) fa[i] = i;
auto same = [&](int x, int y) -> bool {
x = findx(x); y = findx(y);
return x == y;
};
auto unite = [&](int x, int y) {
x = findx(x); y = findx(y);
fa[x] = y;
};
int l = ;
rep(i, , q) {
while (l < m && calc(e[l]) <= b[i].b) {
unite(e[l].u, e[l].v);
l ++;
}
if (same(b[i].x, b[i].y)) b[i].ans = ;
}
sort(b + , b + + q, [&](Query &a, Query &b) {
return a.id < b.id;
});
rep(i, , q) printf("%s\n", b[i].ans? "TAK": "NIE");
}
/*
6 4 5
1 5 2 6
1 3 1
2 3 2
3 4 3
4 5 5
6 4 5
4
1 2 4
2 6 9
1 5 9
6 5 8 */

XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--B.Petrol的更多相关文章

  1. XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--J.Cave

    给你一棵树,现在有m个专家,每个专家计划从$a_i$走到$b_i$, 经过的距离不超过$d_i$,现在让你找一个点,使得所有专家的路途都能经过这个点 令$S_i$表示满足第i个专家的所有点,先检查1可 ...

  2. XV Open Cup named after E.V. Pankratiev. GP of Tatarstan

    A. Survival Route 留坑. B. Dispersed parentheses $f[i][j][k]$表示长度为$i$,未匹配的左括号数为$j$,最多的未匹配左括号数为$k$的方案数. ...

  3. XV Open Cup named after E.V. Pankratiev. GP of America

    A. Area of Effect 首先最优解中必有一个点在圆的边界上. 若半径就是$R$,则枚举一个点,然后把剩下的事件极角扫描即可,时间复杂度$O(m(n+m)\log(n+m))$. 否则圆必然 ...

  4. XV Open Cup named after E.V. Pankratiev. GP of Three Capitals

    A. Add and Reverse 要么全部都选择$+1$,要么加出高$16$位后翻转位序然后再补充低$16$位. #include<stdio.h> #include<iostr ...

  5. XV Open Cup named after E.V. Pankratiev. GP of Siberia-Swimming

    给出两个点,找到过这两个点的等角螺线,并求出中间的螺线长 $c = \frac{b}{a}$ $p = a \times c^{\frac{\theta}{angle}}$ 对弧线积分 #includ ...

  6. XII Open Cup named after E.V. Pankratiev. GP of Eastern Europe (AMPPZ-2012)

    A. Automat $m$超过$1600$是没用的. 从后往前考虑,设$f[i][j][k]$表示考虑$[i,n]$这些物品,一共花费$j$元钱,买了$k$个物品的最大收益. 时间复杂度$O(n^5 ...

  7. XV Open Cup named after E.V. Pankratiev Stage 6, Grand Prix of Japan Problem J. Hyperrectangle

    题目大意: 给出一个$d$维矩形,第i维的范围是$[0, l_i]$. 求满足$x_1 + x_2 + ...x_d \leq s$ 的点构成的单纯形体积. $d, l_i \leq 300$ 题解: ...

  8. XVI Open Cup named after E.V. Pankratiev. GP of Eurasia

    A. Nanoassembly 首先用叉积判断是否在指定向量右侧,然后解出法线与给定直线的交点,再关于交点对称即可. #include<bits/stdc++.h> using names ...

  9. XVI Open Cup named after E.V. Pankratiev. GP of SPB

    A. Bubbles 枚举两个点,求出垂直平分线与$x$轴的交点,答案=交点数+1. 时间复杂度$O(n^2\log n)$. #include<cstdio> #include<a ...

随机推荐

  1. node概述

    1.什么是node:“一个搭建在Chrome JavaScript运行时 上的平台,用于构建高速.可伸缩的网络程序.Node.js采用的事件驱动.非阻塞I/O模型,使它 既轻量又高效,并成为构建运行在 ...

  2. jupyter notebook + frp 实现内容穿透

    服务器上找到frps.ini 配置如下 [common] bind_port = 7000 vhost_http_port = 8890 要穿透的笔记本的frpc.ini配置 [common] ser ...

  3. python:sys.exit() os._exit() exit() quit()

    1>sys.exit() >>> import sys>>> help(sys.exit)Help on built-in function exit in ...

  4. react-native 导入高德地图

    高德官网 : https://lbs.amap.com/ GitHub地址: https://github.com/qiuxiang/react-native-amap3d 安装的时候遇到错误:一般是 ...

  5. webpack4 坑收集:html-webpack-plugin在多页面时,无法将optimization.splitChunks提取的公共块,打包到页面中

    问题描述:  有2个页面index.html和product.html,用html-webpack-plugin和optimization.splitChunks的基本配置如下 { template: ...

  6. python 科学计算及数据可视化

    第一步:利用python,画散点图. 第二步:需要用到的库有numpy,matplotlib的子库matplotlib.pyplot numpy(Numerical Python extensions ...

  7. 小容量的byteBuffer 读取大文本

    利用死循环和判断是否 读到0个字节,便能判断是否读取完成,但它存在如下问题,如果输入是中文的话,可能没有读取完中文的全部3个字节,导致乱码.如果数据足够随机,这样的情况肯定会出现的 @Test pub ...

  8. JAVA获取计算机IP地址

    import java.net.InetAddress;import java.net.UnknownHostException;public class HuoQu {    public stat ...

  9. eclipse上的.properties文件中文编辑显示问题

    安装 装Properties Editor插件,方法: Help --> Install New Software -->输入:http://propedit.sourceforge.jp ...

  10. Java基础学习-常量和变量

    1.常量概述     -在程序执行的过程中,其值不可以发生改变的量. 2.常量的分类     -字符串常量    用双引号括起来的内容("HelloWorld")     -整数常 ...