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 )
水题... 排序搞出每天的会议有哪些, 然后再按照会议的开始时间和结束时间排序, 最晚开始的和最早结束的会议不是同一场而且最晚开始的时间>最早结束的会议就有可能方案 -------------- ...
随机推荐
- zlog学习笔记(mdc)
mdc.h #ifndef __zlog_mdc_h #define __zlog_mdc_h #include "zc_defs.h" typedef struct zlog_m ...
- cell与cell之间的间距问题,以及section跟随屏幕滑动而滑动问题
苹果在cell与cell之间默认没有间距,这样有时候不能满足我们界面要求,所以我们就需要将cell设置为分组模式(也就是每组一行或者多行,分为n组),然后我们就可以在代理中根据自己的需求设计cell之 ...
- 005商城项目:ssm框架的整合成功之后的问题:使用maven的tomcat插件时的debug
在执行maven的clean时经常碰到一个问题: 解决: 然后: 还有一个问题是:用maven 使用Debug调试时,不能看到源代码. 解决办法: 下面选择Debug Configurations 这 ...
- iptables案例手册
Linux防火墙Iptable如何设置只允许某个ip访问80端口,只允许特定ip访问某端口 iptables常用实例备查(更新中) 9个常用iptables配置实例 案例: http://www.cn ...
- Map集合 总结
(本人第一次写博客,部分内容有参照李刚老师的疯狂java系列图书,如有遗漏错误,请多指教,谢谢.) Java的集合类可分为Set.List.Map.Queue,其中Set.List.Queue都有共同 ...
- MvvmLight ToolKit .Net4.5版本 CanExecute不能刷新界面bug
一 问题重现 1.在使用最新版本v5.1的MvvmLight中(其实这个问题很早就有了),发现CanExecute不能很好地工作了.一个简单的工程,只有MainWindow和MainWindow ...
- 如何在batch脚本中嵌入python代码
老板叫我帮他测一个命令在windows下消耗的时间,因为没有装windows那个啥工具包,没有timeit那个命令,于是想自己写一个,原理很简单: REM timeit.bat echo %TIME% ...
- shell 使用
echo -e "1\t2\t3" #-e echo -e "\e[1;31m This is red text \e[0m" #color echo -e & ...
- c++虚函数注意事项
>在基类方法声明中使用关键字virtual,可以使该方法在基类及所有的派生类中是虚的 >如果使用指向对象的引用或指针来调用虚方法,程序将使用对象类型定义的方法,而不使用为引用或指针类型定义 ...
- 生成短链(网址) ShortUrlLink
建表 CREATE TABLE [dbo].[ShortUrl]( [Id] [,) NOT NULL, [LongUrl] [nvarchar]() NOT NULL, [BaseUri] [int ...