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 )
水题... 排序搞出每天的会议有哪些, 然后再按照会议的开始时间和结束时间排序, 最晚开始的和最早结束的会议不是同一场而且最晚开始的时间>最早结束的会议就有可能方案 -------------- ...
随机推荐
- 用Docker封装一个web应用(Django)
一.复用以前一个封装了SSH的镜像,如果没有封装SSH,可以使用自己的镜像或参考我以前博文:叫板OpenStack:用Docker实现私有云 的前五步 接下来便是正题. 二.部署过程 1.查看镜像 R ...
- 迭代器和for-of循环 顺便带一下Es5中的.map遍历
let set = new Set(); //set方法去除重复的数据 [1, 2, 3, 4, 2, 8, 4].map(function (elem) { set.add(elem); //遍历完 ...
- 两个经典的Oracle触发器示例(轉)
[案例一] 题目:--触发器:--添加员工信息,流水号作为自动编号(通过序列生成),--并且判断如果工资小于0,则改为0;如果大于10000,则改为10000. CREATE TABLE emp2(e ...
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...
- css默认值汇总
原文:http://www.cnblogs.com/xiangzi888/archive/2011/10/12/2209514.html HTML标签CSS属性默认值汇总 这个东西,在你需要还原默认值 ...
- ASP.NET整理:Cookie,Application,Session,页面生命周期
一.设置Cookie的2种方式 1. Repsonse.Cookie[“名”] = 值; 2. HttpCookie hcCookie = new HttpCookie(“名”,值); h ...
- mac系统上使用压缩包版的mysql(非安装版)
mac本换了块固态硬盘,一切重新装过,mysql嫌官网下载太慢,直接百度 "mysql mac",第一个就是: 不料下载完后,发现这是一个压缩包版,并没有安装程序.网上搜索了一下, ...
- 代码滑动panorama-即程序中设置SelectedIndex
我们都知道panorama的SelectedIndex属性是只读的,所以通过修改它,在程序滑动panorama似乎不可能.那么是不是就没有办法了呢?其实我们可以通过设置SelectedItemProp ...
- C118+Osmocom-bb+Openbts搭建小型基站
演示图片: 演示视频: 交流论坛:GsMsEc 交流Q群:
- Http概述(一)
Http使用的是可靠的数据传输协议,因此即使数据来自地球的另一端,也能够确保数据在传输过程中不会被损坏或产生混乱. 这样用户在访问信息时就不用担心其完整性了. web服务端与服务器是如何通信的 Web ...