BZOJ 4144: [AMPPZ2014]Petrol
4144: [AMPPZ2014]Petrol
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 457 Solved: 170
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
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
Sample Output
TAK
TAK
NIE
HINT
Source
分析
为了回答每个询问,我们需要加油站之间的最小生成树。
求最小生成树的方式是:让所有的加油站dis为0,做多源最短路,同时记录距离每个点最近的加油站。然后枚举边,可以得到两个加油站之间的可能最短距离。做Kruskal或Prim即可。
求到最小生成树之后,用倍增法维护路径上的最大权值即可。
代码
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define N 500005
#define inf 0x3f3f3f3f int n, m, s, c[N]; struct edge
{
int x, y, w; edge(void) {};
edge(int _x, int _y, int _w) :
x(_x), y(_y), w(_w) {}; friend bool operator < (const edge &a, const edge &b)
{
return a.w < b.w;
}
}; struct step
{
int id, dis; step(void) {};
step(int a, int b) :
id(a), dis(b) {}; friend bool operator < (const step &a, const step &b)
{
return a.dis > b.dis;
}
}; namespace kirito
{
edge e[N]; int edge_cnt = ; int hd[N], to[N], vl[N], nt[N], tot; void addEdge(int x, int y, int w)
{
nt[tot] = hd[x]; to[tot] = y; vl[tot] = w; hd[x] = tot++;
nt[tot] = hd[y]; to[tot] = x; vl[tot] = w; hd[y] = tot++;
} int dis[N], from[N]; priority_queue<step> pq;
} namespace masiro
{
edge e[N]; int edge_cnt = ; void pushEdge(int x, int y, int w)
{
e[++edge_cnt] = edge(x, y, w);
} int hd[N], to[N], vl[N], nt[N], tot; void addEdge(int x, int y, int w)
{
nt[tot] = hd[x]; to[tot] = y; vl[tot] = w; hd[x] = tot++;
nt[tot] = hd[y]; to[tot] = x; vl[tot] = w; hd[y] = tot++;
} int fa[N]; int find(int u)
{
return fa[u] == u ? u : fa[u] = find(fa[u]);
} int root; int dep[N], fat[N][], mex[N][]; void predfs(int u, int f)
{
for (int i = ; i < ; ++i)
{
fat[u][i] = fat[fat[u][i - ]][i - ];
mex[u][i] = max(mex[u][i - ], mex[fat[u][i - ]][i - ]);
} for (int i = hd[u]; ~i; i = nt[i])
if (to[i] != f)
{
dep[to[i]] = dep[u] + ;
mex[to[i]][] = vl[i];
fat[to[i]][] = u;
predfs(to[i], u);
}
}
} void prework1(void)
{
using namespace kirito; memset(dis, inf, sizeof(dis)); for (int i = ; i <= s; ++i)
{
dis[c[i]] = ;
from[c[i]] = c[i];
pq.push(step(c[i], ));
} while (!pq.empty())
{
step top = pq.top(); pq.pop(); if (dis[top.id] != top.dis)
continue; for (int i = hd[top.id]; ~i; i = nt[i])
if (dis[to[i]] > vl[i] + top.dis)
{
from[to[i]] = from[top.id];
dis[to[i]] = vl[i] + top.dis;
pq.push(step(to[i], dis[to[i]]));
}
} for (int i = ; i <= m; ++i)
if (from[e[i].x] ^ from[e[i].y])
masiro::pushEdge(from[e[i].x], from[e[i].y], dis[e[i].x] + dis[e[i].y] + e[i].w);
} void prework2(void)
{
using namespace masiro; sort(e + , e + + edge_cnt); for (int i = ; i <= n; ++i)
fa[i] = i; for (int i = ; i <= edge_cnt; ++i)
{
int fx = find(e[i].x);
int fy = find(e[i].y); if (fx ^ fy)
{
fa[fx] = fy;
addEdge(e[i].x, e[i].y, e[i].w);
}
} root = n + ; for (int i = ; i <= s; ++i)
if (find(c[i]) == c[i])
addEdge(root, c[i], inf); dep[root] = ;
fat[root][] = root;
memset(mex, , sizeof(mex)); predfs(root, -);
} int lca(int x, int y)
{
using namespace masiro; int res = ; if (dep[x] < dep[y])
swap(x, y); for (int i = ; i >= ; --i)
if (dep[fat[x][i]] >= dep[y])
{
res = max(res, mex[x][i]);
x = fat[x][i];
} if (x == y)
return res; for (int i = ; i >= ; --i)
if (fat[x][i] != fat[y][i])
{
res = max(res, mex[x][i]);
res = max(res, mex[y][i]);
x = fat[x][i];
y = fat[y][i];
} res = max(res, mex[x][]);
res = max(res, mex[y][]); return res;
} signed main(void)
{
scanf("%d%d%d", &n, &s, &m); for (int i = ; i <= s; ++i)
scanf("%d", c + i); memset(kirito::hd, -, sizeof(kirito::hd));
memset(masiro::hd, -, sizeof(masiro::hd)); for (int i = ; i <= m; ++i)
{
int x, y, w; scanf("%d%d%d", &x, &y, &w); kirito::addEdge(x, y, w);
kirito::e[i] = edge(x, y, w);
} prework1();
prework2(); int q; scanf("%d", &q); for (int i = ; i <= q; ++i)
{
int x, y, w; scanf("%d%d%d", &x, &y, &w); int maxCost = lca(x, y); if (w >= maxCost)
puts("TAK");
else
puts("NIE");
}
}
BZOJ_4144.cpp
@Author: YouSiki
BZOJ 4144: [AMPPZ2014]Petrol的更多相关文章
- BZOJ.4144.[AMPPZ2014]Petrol(Kruskal重构树)
BZOJ 看别人代码的时候发现哪一步都很眼熟,突然想起来,就在四个月前我好像看过还给别人讲过?mmp=v= 果然不写写就是容易忘.写了好歹忘了的时候还能复习呢(虽然和看别人的好像也没多少差别?). 首 ...
- 4144: [AMPPZ2014]Petrol (多源最短路+最小生成树+启发式合并)
4144: [AMPPZ2014]Petrol Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 752 Solved: 298[Submit][Sta ...
- 【BZOJ】4144: [AMPPZ2014]Petrol
题意 给定一个\(n\)个点.\(m\)条边的带权无向图,其中有\(s\)个点是加油站.每辆车都有一个油量上限\(b\),即每次行走距离不能超过\(b\),但在加油站可以补满.\(q\)次询问,每次给 ...
- [BZOJ 4144] Petrol
Link: BZOJ 4144 传送门 Solution: 一道不错的图论综合题 因为只询问关键点,因此重点是要求出关键点之间的最短路,以最短路建图 记$nst[i]$为离$i$最近的关键点:可以发现 ...
- 【BZOJ4144】[AMPPZ2014]Petrol 最短路+离线+最小生成树
[BZOJ4144][AMPPZ2014]Petrol Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油 ...
- bzoj 4152[AMPPZ2014]The Captain
bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )
先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...
- BZOJ 4143: [AMPPZ2014]The Lawyer( sort )
水题... 排序搞出每天的会议有哪些, 然后再按照会议的开始时间和结束时间排序, 最晚开始的和最早结束的会议不是同一场而且最晚开始的时间>最早结束的会议就有可能方案 -------------- ...
随机推荐
- li标签包含img的问题
我们在制作页面时,经常有可能碰到这样的设计: li 图一 图一的布局代码如下: <ul> <li><img src=”pic1.jpg” />& ...
- Mysql备份系列(1)--备份方案总结性梳理
mysql数据库备份有多么重要已不需过多赘述了,废话不多说!以下总结了mysql数据库的几种备份方案: 一.binlog二进制日志通常作为备份的重要资源,所以再说备份方案之前先总结一下binlog日志 ...
- 叫板OpenStack:用Docker实现私有云
看到各大厂商的云主机,会不会觉得高大上?目前大公司的主流方案是OpenStack,比如某个公司的私有云
- PAT 1019. 数字黑洞 (20)
给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字.一直重复这样做,我们很快会停在有"数字黑洞&qu ...
- 用Myisamchk让MySQL数据表更健康
用Myisamchk让MySQL数据表更健康 2011-03-15 09:15 水太深 ITPUB 字号:T | T 为了让MySQL数据库中的数据表“更健康”,就需要对其进行定期体检.在这里笔者推荐 ...
- DEDECMS之三 首页、列表页怎么调用文章内容
一.首页调用 百度了很多,没有找到实际的解决方法,对于直接读取数据库,这种写法不会采取. 后来,仔细考虑,这部分解决的内容不会很多,所以直接使用了简介的内容 方法一(默认长度55) [field:in ...
- 解决jquery.validate.js的验证bug
版本提示:jq为1.4.4, jquery.validate 为jQuery validation plug-in 1.7 问题: a.选填选项,如邮箱设置格式验证,那么情况输入框,验证label变成 ...
- T138
这一列车. 十年前送我去西安, 十年后搭我返故乡. 十年前手拉着手儿, 十年后独对着车窗. 这一列车. 装饰着坚毅的中国蓝, 却失去了往日光环. 只有通往偏远.落后的地方, 只有没赶上高铁动车的行 ...
- codevs 3369 膜拜
3369 膜拜 http://codevs.cn/problem/3369/ 题目描述 Description 神牛有很多-当然-每个同学都有自己衷心膜拜的神牛.某学校有两位神牛,神牛甲和神牛乙.新入 ...
- 大圆满的精髓–肯•威尔伯(KEN WILBER)
作者:肯·威尔伯(Ken Wilber),目前被公认为是“后人本心理学”的最重要的思想家.理论家和发言人,其影响已经跨越了心理学领域,波及哲学和神学领域.由于肯·威尔伯在意识领域的研究极富基础性和开创 ...