bzoj 4289 Tax - 最短路
题目传送门
这是一条通往vjudge的神秘通道
这是一条通往bzoj的神秘通道
题目大意
如果一条路径走过的边依次为$e_{1}, e_{2}, \cdots , e_{k}$,那么它的长度为$e_{1} + \max (e_{1}, e_{2}) + \max (e_{2}, e_{3}) + \cdots + \max (e_{k - 1}, e_{k}) + e_{k}$,问点$1$到点$n$的最短路。
显然需要把状态记在最后一条边上。
然后给一个菊花图,这个做法就gg了。
因此考虑一些黑科技。
可以把边看成点,然后考虑如何在辺与边之间快速连边。
对于一个点的出边,可以把它们按照权值排序,大的向比它略小的连一条权值为0的边,小的向比它略大的连一天权值为它们边权之差的边。
然后原图中每条边向它的反向边连一条边权相同的边。
然后再建两个虚点,一个起点,向以1为起点的边连边,边权不变。一个终点,以$n$为终点的边向它连边。
Code
/**
* bzoj
* Problem#4289
* Accepted
* Time: 3200ms
* Memory: 31292k
*/
#include <bits/stdc++.h>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean; #define ll long long typedef class Edge {
public:
int end, next, w; Edge(int end = , int next = , int w = ):end(end), next(next), w(w) { }
}Edge; typedef class MapManager {
public:
int ce;
int *h;
Edge *es; MapManager() { }
MapManager(int n, int m):ce(-) {
h = new int[(n + )];
es = new Edge[(m + )];
memset(h, -, sizeof(int) * (n + ));
} void addEdge(int u, int v, int w) {
es[++ce] = Edge(v, h[u], w);
h[u] = ce;
// cerr << u << "->" << v << " " << w << endl;
} Edge& operator [] (int pos) const {
return es[pos];
}
}MapManager; int n, m;
Edge *es;
vector<int> *dg;
MapManager g; inline void init() {
scanf("%d%d", &n, &m);
es = new Edge[(m + )];
dg = new vector<int>[(n + )];
for (int i = ; i < m; i++) {
scanf("%d%d%d", &es[i].end, &es[i].next, &es[i].w);
dg[es[i].end].push_back(i << );
dg[es[i].next].push_back(i << | );
}
} boolean cmp(const int& a, const int& b) {
return es[a >> ].w < es[b >> ].w;
} int s, t; inline void build() {
g = MapManager((m << ) + , m << );
s = m << , t = m << | ;
for (int i = ; i <= n; i++) {
sort(dg[i].begin(), dg[i].end(), cmp);
for (int j = ; j < (signed) dg[i].size(); j++) {
int u = (j) ? (dg[i][j - ]) : (-), v = dg[i][j];
g.addEdge(v, u, );
g.addEdge(u, v, es[v >> ].w - es[u >> ].w);
}
} for (int i = ; i < (signed) dg[].size(); i++)
g.addEdge(s, dg[][i], es[dg[][i] >> ].w);
for (int i = ; i < (signed) dg[n].size(); i++)
g.addEdge(dg[n][i] ^ , t, es[dg[n][i] >> ].w); for (int i = , w; i < m; i++) {
w = es[i].w;
g.addEdge(i << , i << | , w);
g.addEdge(i << | , i << , w);
}
} typedef class Node {
public:
int p;
ll dis; Node(int p = , ll dis = ):p(p), dis(dis) { } boolean operator < (Node b) const {
return dis > b.dis;
}
}Node; ll *f;
priority_queue<Node> que;
inline ll dijstra() {
f = new ll[(m << ) + ];
memset(f, 0x3f, sizeof(ll) * ((m << ) + ));
que.push(Node(s, f[s] = ));
while (!que.empty()) {
Node e = que.top();
que.pop();
if (e.dis != f[e.p]) continue;
for (int i = g.h[e.p]; ~i; i = g[i].next) {
Node eu (g[i].end, e.dis + g[i].w);
if (eu.dis < f[eu.p]) {
f[eu.p] = eu.dis;
que.push(eu);
}
}
}
return f[t];
} inline void solve() {
printf(Auto"\n", dijstra());
} int main() {
init();
build();
solve();
return ;
}
bzoj 4289 Tax - 最短路的更多相关文章
- bzoj 4289 TAX —— 点边转化
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 把边转化成点,同一个原有点相连的边中,边权小的向大的连差值的边,大的向小的连0的边: ...
- 「BZOJ 4289」 PA2012 Tax
「BZOJ 4289」 PA2012 Tax 题目描述 给出一个 \(N\) 个点 \(M\) 条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点 \(1\) 到点 \( ...
- BZOJ 4289: PA2012 Tax 差分建图 最短路
https://www.lydsy.com/JudgeOnline/problem.php?id=4289 https://www.cnblogs.com/clrs97/p/5046933.html ...
- BZOJ 4289: PA2012 Tax(最短路)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 755 Solved: 240[Submit][Status][Discuss] Descriptio ...
- ●BZOJ 4289 PA2012 Tax
●赘述题目 算了,题目没有重复的必要. 注意理解:对答案造成贡献的是每个点,就是了. 举个栗子: 对于如下数据: 2 1 1 2 1 答案是 2: ●题解 方法:建图(难点)+最短路. 先来几个链接: ...
- BZOJ.4289.PA2012 Tax(思路 Dijkstra)
题目链接 \(Description\) 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价 ...
- bzoj 4289 PA2012 Tax——构图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 可以把一个点上的边按权值排序,然后边权小的向第一个比它大的连差值的边,边权大的向第一个 ...
- bzoj 4289: PA2012 Tax
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...
- 【刷题】BZOJ 4289 PA2012 Tax
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...
随机推荐
- mx:Panel (面板容器) mx:Button (按钮) 默认大小
1.默认组件大小 <mx:Panel title="默认的面板容器大小和按钮控件大小"> <!-- 使用控件大小默认值 --> <mx:Button ...
- ubuntu安装rvm
sudo apt-get install curl git-core bash -s stable < <(curl -s https://raw.github.com/wayneeseg ...
- ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' error on debug
现象:pycharm调试代码出现错误:ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' error on debu ...
- C# Activator和new的区别
1.你需要动态的创建一个实例模型的时候,就用Activator.CreateInstance(Type type);如果是明确的知道要创建哪个实例的模型,就可以用 new Class1()了. T t ...
- JavaScript--函数、匿名函数和自执行函数详解
函数的声明及调用 1.函数的声明格式: function 函数名([参数1],[参数2],.....){ //函数体代码 [return 返回值 ...
- hive中的with用法
hive 可以通过with查询来提高查询性能,因为先通过with语法将数据查询到内存,然后后面其它查询可以直接使用,这种方法与创建临时表类似但是不需要创建临时表实体表,内存中的子查询结果在会话结束后会 ...
- scrapy框架 + selenium 爬取豆瓣电影top250......
废话不说,直接上代码..... 目录结构 items.py import scrapy class DoubanCrawlerItem(scrapy.Item): # 电影名称 movieName = ...
- c# 继承与多种状态
可访问性: public 无限制 internal 只允许在同一个程序集访问 protected ...
- Django高级
一 登录装饰器 def login_required(view_func): '''登录判断装饰器''' def wrapper(request, *view_args, **view_kwargs) ...
- UVA - 748 Exponentiation
Problems involving the computation of exact values of very large magnitude and precision are common. ...