多源最短路+并查集

#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. 腾讯笔试编程题,贪吃的小Q(二分查找)

    问题描述 小Q的父母要出差N天,走之前给小Q留下了M块巧克力.小Q决定每天吃的巧克力数量不少于前一天吃的一半,但是他又不想在父母回来之前的某一天没有巧克力吃,请问他第一天最多能吃多少块巧克力. 输入描 ...

  2. CentOS7 配置Mailx使用SMTP发送邮件

    mail.mailx和sendmail介绍: mail是mailx的别名,最初叫nail(与arch linux目前用的S-nail基因相同):mail是Heirloom的一个子项目.sendmail ...

  3. 百度API经纬度和地址互相查询

    /** * 获取地址对应的坐标 * @param $address * @return array */ function getAddressPoint($address){ $lng = 0; $ ...

  4. JDK8到JDK12各个版本的重要特性整理

    JDK8新特性 1.Lambda表达式 2.函数式编程 3.接口可以添加默认方法和静态方法,也就是定义不需要实现类实现的方法 4.方法引用 5.重复注解,同一个注解可以使用多次 6.引入Optiona ...

  5. linux 分析java 线程状态

    将线程3117 的线程消息放到文件dump17中 jstack 13492 > dump17 分析线程 grep java.lang.Thread.State dump17 | awk '{pr ...

  6. BottomNavigationBarItem fixed

    BottomNavigationBar( type: BottomNavigationBarType.fixed, onTap: (value){ if more then 3 items,, use ...

  7. Vue 旅游网首页开发3 - Ajax获取首页数据

    之前的首页数据都是写死在页面上的,现在修改项目,使得数据通过ajax动态获取. 死胎了 ... 不想写了····

  8. day 07

    # 数据类型的相互转化# 字符编码# 文件操作 # 1.哪些类型可以转化为数字# res = int('10')# print(res)# res = int('-3')# print(res)# r ...

  9. 利用matplotlib库和numpy库画数学图形

    首先,电脑要安装到matplotlib库和numpy库,这可以通过到命令符那里输入“pip install matplotlib ”,两个操作一样 其次,参照下列代码: import numpy as ...

  10. IDEA上的项目托管到码云步骤

    IDEA上的项目托管到码云步骤:1.安装Git2.idea上配置Git    Setting-Version Control-Git    把git.exe改为安装的Git的执行路径如:D:\Prog ...