BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】
题目链接
题解
这题好妙啊,,orz
假设我们在一个非加油站点,那么我们一定是从加油站过来的,我们剩余的油至少要减去这段距离
如果我们在一个非加油站点,如果我们到达不了任意加油站点,我们一定废了
那么我们在一个非加油站点,就一定可以到达最近的加油站,而由于我们剩余的油是要减去到加油站距离的,所以我们剩余的油一定是\(b - d\),\(d\)表示到达最近加油站的距离。假如我们没有那么多油,我们一定可以开过去再回来,就有了
因此,我们在任意一个点的油量确定,两点之间可以直达,当且仅当
①两点间有边
②\(w + d[u] + d[v] \le b\)
因此可以以此为新边权跑最小生成树,同时离线回答询问
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 200005,maxm = 400005,INF = 2000000026;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,s,m,Q,c[maxn];
int h[maxn],ne;
struct EDGE{int to,nxt,w;}ed[maxm];
inline void build(int u,int v,int w){
ed[++ne] = (EDGE){v,h[u],w}; h[u] = ne;
ed[++ne] = (EDGE){u,h[v],w}; h[v] = ne;
}
struct edge{int a,b,w;}e[maxn];
inline bool operator < (const edge& a,const edge& b){
return a.w < b.w;
}
struct Que{int u,v,b,id;}q[maxn];
inline bool operator < (const Que& a,const Que& b){
return a.b < b.b;
}
priority_queue<cp,vector<cp>,greater<cp> > qu;
int vis[maxn],d[maxn];
void dijkstra(){
for (int i = 1; i <= n; i++) d[i] = INF;
for (int i = 1; i <= s; i++)
d[c[i]] = 0,qu.push(mp(0,c[i]));
int u;
while (!qu.empty()){
u = qu.top().second; qu.pop();
if (vis[u]) continue;
vis[u] = true;
Redge(u) if (!vis[to = ed[k].to] && d[u] + ed[k].w < d[to]){
d[to] = d[u] + ed[k].w;
qu.push(mp(d[to],to));
}
}
}
int ans[maxn],pre[maxn];
inline int find(int u){return u == pre[u] ? u : pre[u] = find(pre[u]);}
void kruskal(){
REP(i,m) e[i].w = e[i].w + d[e[i].a] + d[e[i].b];
sort(e + 1,e + 1 + m);
int t = 1,fa,fb;
REP(i,n) pre[i] = i;
for (int i = 1; i <= m; i++){
while (t <= Q && e[i].w > q[t].b){
ans[q[t].id] = (find(q[t].u) == find(q[t].v));
t++;
}
fa = find(e[i].a); fb = find(e[i].b);
if (fa != fb) pre[fb] = fa;
}
while (t <= Q){
ans[q[t].id] = (find(q[t].u) == find(q[t].v));
t++;
}
}
int main(){
n = read(); s = read(); m = read();
REP(i,s) c[i] = read();
REP(i,m){
e[i].a = read(); e[i].b = read(); e[i].w = read();
build(e[i].a,e[i].b,e[i].w);
}
Q = read();
REP(i,Q) q[i].u = read(),q[i].v = read(),q[i].b = read(),q[i].id = i;
sort(q + 1,q + 1 + Q);
dijkstra();
kruskal();
REP(i,Q) puts(ans[i] ? "TAK" : "NIE");
return 0;
}
BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】的更多相关文章
- BZOJ4144: [AMPPZ2014]Petrol(最短路 最小生成树)
题意 题目链接 Sol 做的时候忘记写题解了 可以参考这位大爷 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...
- 【BZOJ4144】[AMPPZ2014]Petrol 最短路+离线+最小生成树
[BZOJ4144][AMPPZ2014]Petrol Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油 ...
- [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= 果然不写写就是容易忘.写了好歹忘了的时候还能复习呢(虽然和看别人的好像也没多少差别?). 首 ...
随机推荐
- OSG-获取OSG的源代码和第三方库并编译
获取OSG的源代码有很多方式. 这里说下其中的两个地方,第一就是中国的OSG网站http://www.osgchina.org/,这个网站目前应该是由中国西安恒歌科技维护,同时,西安恒歌科技也是一家已 ...
- OSG-简单模型控制
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- C do whlie 数数位
#include <stdio.h> int main(int argc, char **argv) { //定义两个变量 x 跟 n,n的初始化为0: int x; int n ...
- C 计算员工工资
#include <stdio.h> int main(int argc, char **argv) { //定义四个变量 g每小时固定的工资 40 固定工作时间 pay工资 hours员 ...
- centos端口管理
centos 6.5 ###############配置filter表防火墙############### #清除预设表filter中的所有规则链的规则iptables -F #清除预设表filter ...
- 从oracle导入hive
sqoop import --connect jdbc:oracle:thin:@10.39.1.43:1521/rcrm --username bi_query --password ####### ...
- ACM入门步骤(一)
一般的入门顺序: 0. C语言的基本语法(或者直接开C++也行,当一个java选手可能会更受欢迎,并且以后工作好找,但是难度有点大),[参考书籍:刘汝佳的<算法竞赛入门经典>,C++入门可 ...
- 拓扑排序(Toposort)
摘自:https://blog.csdn.net/qq_35644234/article/details/60578189 <图论算法> 1.拓扑排序的介绍 对一个有向无环图(Direct ...
- Reversing Encryption(模拟水题)
A string ss of length nn can be encrypted(加密) by the following algorithm: iterate(迭代) over all divis ...
- 美美哒rand()函数
2016.3.7 天气:大雪都已经三月份竟然还下了这么大的雪,真是少见呀.今天为了提交软件工程的作业我们需要注册git的账号,真是前途艰难呀,后台服务器都要爆炸了,其实我觉得这个平台的服务器 ...