BZOJ4144: [AMPPZ2014]Petrol(最短路 最小生成树)
题意
Sol
做的时候忘记写题解了
可以参考这位大爷
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second
using namespace std;
const int MAXN = 2e6 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, S, M, Q, c[MAXN], vis[MAXN], ga[MAXN], ans[MAXN], fa[MAXN], dis[MAXN];
vector<Pair> v[MAXN];
struct Edge {
int u, v, w;
bool operator < (const Edge &rhs) const {
return w < rhs.w;
}
}E[MAXN];
struct Query {
int x, y, b, id;
bool operator < (const Query &rhs) const {
return b < rhs.b;
}
}q[MAXN];
void Dij() {
memset(dis, 0x3f, sizeof(dis));
priority_queue<Pair> q;
for(int i = 1; i <= S; i++) ga[c[i]] = c[i], dis[c[i]] = 0, q.push(MP(0, c[i]));
while(!q.empty()) {
if(vis[q.top().se]) {q.pop(); continue;}
int p = q.top().se; q.pop(); vis[p] = 1;
for(int i = 0; i < v[p].size(); i++) {
int to = v[p][i].fi, w = v[p][i].se;
if(dis[to] > dis[p] + w)
dis[to] = dis[p] + w, q.push(MP(-dis[to], to)), ga[to] = ga[p];
}
}
}
int find(int x) {
return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
void unionn(int x, int y) {
fa[find(x)] = find(y);
}
void Build() {
for(int i = 1; i <= N; i++) fa[i] = i;
int tmp = 0;
for(int i = 1; i <= M; i++) {
int x = E[i].u, y = E[i].v;
if(ga[x] == ga[y]) continue;
E[++tmp] = (Edge) {ga[x], ga[y], dis[x] + dis[y] + E[i].w};
}
sort(E + 1, E + tmp + 1);
sort(q + 1, q + Q + 1);
int cur = 1;
for(int i = 1; i <= Q; i++) {
while(cur <= tmp && E[cur].w <= q[i].b) unionn(E[cur].u, E[cur].v), cur++;
ans[q[i].id] = (bool)(find(q[i].x) == find(q[i].y));
}
}
int main() {
N = read(); S = read(); M = read();
for(int i = 1; i <= S; i++) c[i] = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read(), z = read();
E[i] = (Edge) {x, y, z};
v[x].push_back(MP(y, z));
v[y].push_back(MP(x, z));
}
Dij();
Q = read();
for(int i = 1; i <= Q; i++) q[i].x = read(), q[i].y = read(), q[i].b = read(), q[i].id = i;
Build();
for(int i = 1; i <= Q; i++) puts(ans[i] ? "TAK" : "NIE");
return 0;
}
/*
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
*/
BZOJ4144: [AMPPZ2014]Petrol(最短路 最小生成树)的更多相关文章
- 【BZOJ4144】[AMPPZ2014]Petrol 最短路+离线+最小生成树
[BZOJ4144][AMPPZ2014]Petrol Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油 ...
- BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】
题目链接 BZOJ4144 题解 这题好妙啊,,orz 假设我们在一个非加油站点,那么我们一定是从加油站过来的,我们剩余的油至少要减去这段距离 如果我们在一个非加油站点,如果我们到达不了任意加油站点, ...
- [BZOJ4144][AMPPZ2014]Petrol[多源最短路+MST]
题意 题目链接 分析 假设在 \(a \rightarrow b\) 的最短路径中出现了一个点 \(x\) 满足到 \(x\) 最近的点是 \(c\) ,那么我们完全可以从 \(a\) 直接走到 \( ...
- bzoj4144 [AMPPZ2014]Petrol
link 题意: 给一个n个点m条边的带权无向图,其中k个点是加油站,每个加油站可以加满油,但不能超过车的油量上限.有q个询问,每次给出x,y,b,保证x,y都是加油站,问一辆油量上限为b的车从x出发 ...
- 4144: [AMPPZ2014]Petrol (多源最短路+最小生成树+启发式合并)
4144: [AMPPZ2014]Petrol Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 752 Solved: 298[Submit][Sta ...
- BZOJ 4144: [AMPPZ2014]Petrol
4144: [AMPPZ2014]Petrol Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 457 Solved: 170[Submit][Sta ...
- Day3 最短路 最小生成树 拓扑排序
Day3 最短路 最小生成树 拓扑排序 (一)最短路 一.多源最短路 从任意点出发到任意点的最短路 1. Floyd \(O(n^3)\) for(int k=1;k<=n;k++) for(i ...
- 【BZOJ4144】[AMPPZ2014]Petrol(最短路+最小生成树+并查集)
Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油站可以补满. q次询问,每次给出x,y,b,表示出发点是 ...
- BZOJ.4144.[AMPPZ2014]Petrol(Kruskal重构树)
BZOJ 看别人代码的时候发现哪一步都很眼熟,突然想起来,就在四个月前我好像看过还给别人讲过?mmp=v= 果然不写写就是容易忘.写了好歹忘了的时候还能复习呢(虽然和看别人的好像也没多少差别?). 首 ...
随机推荐
- 微信小程序组件化实践
Do Not Repeat Yourself 如何提高代码质量,方法有许多:抽象.模块.组件化,我认为它们的中心点都是--Do Not Repeat Yourself. 小程序组件化 我们先看看小程序 ...
- POJ 2248
#include <iostream> #define MAXN 100 #define min __min using namespace std; int tem[MAXN]; int ...
- java命令行编译和运行引用jar包的文件
经常遇到需要添加第三方jar文件的情况.在命令行状态下要加载外部的jar文件非常麻烦,很不好搞,在网上折腾了很久终于搞定了,在这里做个笔记: 2.运行:java -Djava.ext.dirs=./l ...
- VSTO学习(六)——创建Outlook解决方案
本专题概要 引言 Outlook对象模型 自定义Outlook窗体 小结 一.引言 在上一个专题中,为大家简单介绍了下如何创建Word解决方案的,所以本专题中将为大家介绍下Outlook相关的内容.我 ...
- Visual Studio 中的 Office 和 SharePoint 开发
MSDN Library 开发工具和语言 Visual Studio 中的 Office 和 SharePoint 开发 https://msdn.microsoft.com/zh-cn/libra ...
- Oracle 相关查询
--创建用户 create user zzg identified by zzg123; --修改用户的密码 alter user zzg identified by unis; --所有用户所在的表 ...
- 关联更新 Update
Update a set a.Manage_FunctID=b.Manage_FunctID From Manage_PageUrl a Left join Manage_ButtonBar ...
- WPF获取相对位置、坐标的方法
1.获取鼠标在控件中的坐标: private void item_MouseDown(object sender, MouseButtonEventArgs e) { Point point = e. ...
- ADB故障时的一些命令
开发中经常用到adb重启等操作,简单记录一下. 1.重启 adb kill-server adb start-server 2.显示版本号 adb version 3.显示已连接的设备 adb dev ...
- It is likely that the remote side declared peer gone on this JVM
java.net.ConnectException: t3://host:port: Bootstrap to host/host:port failed. It is likely that the ...