XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--B.Petrol
多源最短路+并查集
#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的更多相关文章
- 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可 ...
- XV Open Cup named after E.V. Pankratiev. GP of Tatarstan
A. Survival Route 留坑. B. Dispersed parentheses $f[i][j][k]$表示长度为$i$,未匹配的左括号数为$j$,最多的未匹配左括号数为$k$的方案数. ...
- XV Open Cup named after E.V. Pankratiev. GP of America
A. Area of Effect 首先最优解中必有一个点在圆的边界上. 若半径就是$R$,则枚举一个点,然后把剩下的事件极角扫描即可,时间复杂度$O(m(n+m)\log(n+m))$. 否则圆必然 ...
- XV Open Cup named after E.V. Pankratiev. GP of Three Capitals
A. Add and Reverse 要么全部都选择$+1$,要么加出高$16$位后翻转位序然后再补充低$16$位. #include<stdio.h> #include<iostr ...
- XV Open Cup named after E.V. Pankratiev. GP of Siberia-Swimming
给出两个点,找到过这两个点的等角螺线,并求出中间的螺线长 $c = \frac{b}{a}$ $p = a \times c^{\frac{\theta}{angle}}$ 对弧线积分 #includ ...
- 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 ...
- 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$ 题解: ...
- XVI Open Cup named after E.V. Pankratiev. GP of Eurasia
A. Nanoassembly 首先用叉积判断是否在指定向量右侧,然后解出法线与给定直线的交点,再关于交点对称即可. #include<bits/stdc++.h> using names ...
- XVI Open Cup named after E.V. Pankratiev. GP of SPB
A. Bubbles 枚举两个点,求出垂直平分线与$x$轴的交点,答案=交点数+1. 时间复杂度$O(n^2\log n)$. #include<cstdio> #include<a ...
随机推荐
- [virtualbox] win10与centos共享目录下,nginx访问问题
原文,http://blog.csdn.net/zhezhebie/article/details/73554872 virtualbox自动挂载之后,默认是挂载在/media/sf_WWW下面的: ...
- CentOS 7 搭建Squid代理服务器
Squid安装 官方地址:http://www.squid-cache.org/ [root@DaMoWang ~]# -r6d8f397.tar.gz [root@DaMoWang ~]# -r6d ...
- Vuex 2.0 深入简出
最近面试充斥了流行框架Vue的各种问题,其中Vuex的使用就相当有吸引力.下面我就将自己深入简出的心得记录如下: 1.在vue-init webpack project (创建vue项目) 2.src ...
- lnmp 安装opencart出现open_basedir 错误解决办法
php 在引用上级目录以外的文件时报错可以通过设置/usr/local/nginx/conf/fastcgi.conf
- python之dict(或对象)与json之间的互相转化
在Python语言中,json数据与dict字典以及对象之间的转化,是必不可少的操作. 在Python中自带json库.通过import json导入. 在json模块有2个方法, loads():将 ...
- 关于linux系统CPU篇--->上下文切换
1.什么是CPU上下文切换? linux是一个多任务操作系统,它支持远大于CPU数量的任务同时运行,当然这些任务实际上并不是真的同时在运行,而是因为系统在很短的时间内,将CPU轮流分配给它们,造成多任 ...
- 如何在Github创建repository
第一步:登陆Github,点击new repository 第二步:输入相应内容创建 第三步,创建完成,如下.
- 外星人入侵游戏(python代码)
https://github.com/ehmatthes/pcc/tree/master/chapter_12/images
- 中间件详解,Django复习
day74 2018-05-21 课程安排周一: 中间件 auth模块+分析BBS项目需求(小组讨论把表结构设计出来) 1. 今日内容 中间件:http://www.cnblogs.com/liwen ...
- fcntl设置FD_CLOEXEC标志作用【转】
本文转载自:https://blog.csdn.net/ustc_dylan/article/details/6930189 通过fcntl设置FD_CLOEXEC标志有什么用?close on ex ...