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= 果然不写写就是容易忘.写了好歹忘了的时候还能复习呢(虽然和看别人的好像也没多少差别?). 首 ...
随机推荐
- 三年同行,质造未来,腾讯WeTest五大服务免费体验
WeTest 导读 2018年10月26日,腾讯WeTest将正式迎来三周岁生日.三周年庆典期间,只要在WeTest平台注册的用户,均可免费体验标准兼容.云真机.压测大师.手游安全扫描.应用安全扫描等 ...
- TW实习日记:第28天
同前两天一样,等接口,开发,调试接口.重复地做着低级代码得搬运工作,确实挺没意思的.怪不得有些人一直说写低级代码很无聊,没有创造性和成就感.31号准备溜了,还是好好复习准备秋招吧. 挖坑清单: Vue ...
- JavaScript 常用正则示例
1. trim功能(清除字符串两端空格) String.prototype.trim = function() { return this.replace(/(^\s+)|(\s+$)/g, '') ...
- leetcode-回文链表
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂 ...
- 【springmvc+mybatis项目实战】杰信商贸-4.maven依赖+PO对+映射文件
上一篇我们附件的增删改查功能全部完成.但是我们的附件有一个字段叫做“类型”(ctype),这里我们要使用数据字典,所以对于这一块我们要进行修改. 首先介绍一下数据字典 数据字典它是一个通用结构,跟业务 ...
- (原) MaterialEditor部- UmateriaEditor中 Node编译过程和使用(1)
@author: 白袍小道 转载说明原处 插件同步在GITHUB: DaoZhang_XDZ 最后YY需求(手滑) 1.在理清楚基础套路和细节后,自定义纹理资源,并加入到现有UE材质系统 2. ...
- JAVA基础:ArrayList和LinkedList区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList ...
- django 连接mysql报错
原因: 问题1. 即从mysql5.7版本之后,默认采用了caching_sha2_password验证方式. 问题2. 然后在执行 python manage.py makemigrations依 ...
- ThinkPHP - 5 - 学习笔记(2015.4.15)
ThinkPHP __construct()和__initialize() 1.__initialize()不是php类中的函数,php类的构造函数只有__construct().2.类的初始化:子类 ...
- lintcode-149-买卖股票的最佳时机
149-买卖股票的最佳时机 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. 样例 给出一个数组样例 [3 ...