BZOJ1576 [Usaco2009 Jan]安全路经Travel
首先用Dijkstra做出最短路生成树,设dis[p]为1到p点的最短路长度
对于一条不在生成树上的边u -> v,不妨设fa为u、v的lca
则一fa到v的路径上的任意点x都可以由u达到,走的方式是1 -> fa -> u -> v -> x,dis'[x] = dis[u] + dis(u, v) + dis[v] - dis[x]
于是可以用dis[u] + dis(u, v) + dis[v]更新fa到v的路径上的所有点
链剖一下,线段树lazytag就好了,连pushdown都不需要
/**************************************************************
Problem: 1576
User: rausen
Language: C++
Result: Accepted
Time:2276 ms
Memory:16636 kb
****************************************************************/ #include <cstdio>
#include <algorithm>
#include <queue> using namespace std;
const int N = 1e5 + ;
const int M = 2e5 + ;
const int inf = 1e9; struct edge {
int next, to, v, vis;
edge(int _n = , int _t = , int _v = , int __v = ) : next(_n), to(_t), v(_v), vis(__v) {}
} e[M << ]; struct heap_node {
int v, e, to;
heap_node(int _v = , int _e = , int _t = ) : v(_v), e(_e), to(_t) {} inline bool operator < (const heap_node &p) const {
return v > p.v;
}
}; struct tree_node {
int fa, sz, dep;
int top, son, e;
int w;
} tr[N]; struct seg_node {
seg_node *ls, *rs;
int mn;
} *seg_root, *seg_null, mempool[N << ], *cnt_seg = mempool; int n, m, cnt_q;
int first[N], tot = ;
int dis[N];
priority_queue <heap_node> h; inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
} inline void Add_Edges(int x, int y, int v) {
e[++tot] = edge(first[x], y, v), first[x] = tot;
e[++tot] = edge(first[y], x, v), first[y] = tot;
} inline void add_to_heap(int p) {
static int x;
for (x = first[p]; x; x = e[x].next)
if (dis[e[x].to] == inf)
h.push(heap_node(e[x].v + dis[p], x, e[x].to));
} void Dijkstra(int S) {
int p;
for (p = ; p <= n; ++p) dis[p] = inf;
while (!h.empty()) h.pop();
dis[S] = , add_to_heap(S);
while (!h.empty()) {
if (dis[h.top().to] != inf) {
h.pop();
continue;
}
p = h.top().to;
dis[p] = h.top().v, e[tr[p].e = h.top().e].vis = ;
h.pop(), add_to_heap(p);
}
} #define y e[x].to
#define Son tr[p].son
void dfs(int p) {
int x;
tr[p].sz = ;
for (x = first[p]; x; x = e[x].next)
if (tr[y].e == x) {
tr[y].dep = tr[p].dep + ;
dfs(y);
tr[p].sz += tr[y].sz;
if (Son == || tr[y].sz > tr[Son].sz) Son = y;
}
} void DFS(int p) {
int x;
tr[p].w = ++cnt_q;
if (Son)
tr[Son].top = tr[p].top, DFS(Son);
for (x = first[p]; x; x = e[x].next)
if (y != Son && tr[y].e == x)
tr[y].top = y, DFS(y);
}
#undef y
#undef Son #define mid (l + r >> 1)
#define Ls p -> ls
#define Rs p -> rs
#define Mn p -> mn
inline void seg_make_null(seg_node *&p) {
p = cnt_seg, Ls = Rs = p, Mn = inf;
} inline void seg_new(seg_node *&p) {
p = ++cnt_seg, *p = *seg_null;
} void seg_build(seg_node *&p, int l, int r) {
seg_new(p);
if (l == r) return;
seg_build(Ls, l, mid), seg_build(Rs, mid + , r);
} void seg_modify(seg_node *p, int l, int r, int L, int R, int v) {
if (L <= l && r <= R) {
Mn = min(Mn, v);
return;
}
if (L <= mid) seg_modify(Ls, l, mid, L, R, v);
if (mid + <= R) seg_modify(Rs, mid + , r, L, R, v);
} int seg_query(seg_node *p, int l, int r, int pos) {
if (l == r) return Mn;
return min(Mn, pos <= mid ? seg_query(Ls, l, mid, pos) : seg_query(Rs, mid + , r, pos));
}
#undef mid
#undef Ls
#undef Rs
#undef Mn int get_lca(int x, int y) {
while (tr[x].top != tr[y].top) {
if (tr[tr[x].top].dep < tr[tr[y].top].dep) swap(x, y);
x = tr[tr[x].top].fa;
}
if (tr[x].dep < tr[y].dep) swap(x, y);
return y;
} void work_modify(int x, int fa, int v) {
while (tr[x].top != tr[fa].top) {
seg_modify(seg_root, , n, tr[tr[x].top].w, tr[x].w, v);
x = tr[tr[x].top].fa;
}
seg_modify(seg_root, , n, tr[fa].w + , tr[x].w, v);
} int main() {
int i, x, y, z, fa, tmp;
n = read(), m = read();
for (i = ; i <= m; ++i) {
x = read(), y = read(), z = read();
Add_Edges(x, y, z);
}
Dijkstra();
for (i = ; i <= n; ++i)
tr[i].fa = e[tr[i].e ^ ].to;
dfs();
tr[].top = , DFS();
seg_make_null(seg_null);
seg_build(seg_root, , n);
#define y e[x].to
for (i = ; i <= n; ++i)
for (x = first[i]; x; x = e[x].next)
if (!e[x].vis) work_modify(y, get_lca(i, y), dis[i] + dis[y] + e[x].v);
#undef y
for (i = ; i <= n; ++i) {
tmp = seg_query(seg_root, , n, tr[i].w);
printf("%d\n", tmp == inf ? - : tmp - dis[i]);
}
return ;
}
BZOJ1576 [Usaco2009 Jan]安全路经Travel的更多相关文章
- 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel
有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...
- BZOJ1576: [Usaco2009 Jan]安全路经Travel(树链剖分)
Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...
- BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)
题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...
- [BZOJ1576] [Usaco2009 Jan]安全路经Travel(堆优化dijk + (并查集 || 树剖))
传送门 蒟蒻我原本还想着跑两边spfa,发现不行,就gg了. 首先这道题卡spfa,所以需要用堆优化的dijkstra求出最短路径 因为题目中说了,保证最短路径有且只有一条,所以可以通过dfs求出最短 ...
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
[BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MB Submit: 665 Solved: 227[Sub ...
- [Usaco2009 Jan]安全路经Travel BZOJ1576 Dijkstra+树链剖分+线段树
分析: Dijkstra求最短路树,在最短路树上进行操作,详情可见上一篇博客:http://www.cnblogs.com/Winniechen/p/9042937.html 我觉得这个东西不压行写出 ...
- bzoj 1576 [Usaco2009 Jan]安全路经Travel(树链剖分,线段树)
[题意] 给定一个无向图,找到1-i所有的次短路经,要求与最短路径的最后一条边不重叠. [思路] 首先用dijkstra算法构造以1为根的最短路树. 将一条无向边看作两条有向边,考察一条不在最短路树上 ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel——并查集+dijkstra
Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...
随机推荐
- python_way,day4 内置函数(callable,chr,随机验证码,ord),装饰器
python_way,day4 1.内置函数 - 下 制作一个随机验证码 2.装饰器 1.内置函数 - 下 callable() #对象能否被调用 chr() #10进制数字对应的ascii码表中的内 ...
- maven使用入门(pom)
mvn clean complie mvn clean test mvn clean package mvn clean install(该任务将该项目输出的jar安装到了Maven本地仓库中) 各个 ...
- HTML5探索之datalist研究
最近一个项目需要用到类似淘宝,百度搜索时的自动检索方案.自然想到了使用datalist标签.但是遇到一个bug,经过两天研究.小有结果给大家分享下~~ 首先看bug吧!~ 因为项目最开始测试就是用36 ...
- jQuery插件开发全解析
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- Python中的__new__()方法的使用
__new__() 函数只能用于从object继承的新式类. 先看下object类中对__new__()方法的定义: class object: @staticmethod # known cas ...
- embed object以及video标签的区别以及使用
embed object以及video标签的区别以及使用?
- Java 中使用 UEditor 整理【待续。。。】
1.简介 官网:http://ueditor.baidu.com/website/index.html 演示:http://ueditor.baidu.com/website/examples/ 2. ...
- eclipse右击打war包class没打上去的问题
今天,遇到个诡异的问题,一个工程eclipse打war包class就是打不上,别的能打上,发现这样选择tomcat,打包成功,谨此备注.
- D3.js 选择元素和绑定数据/使用数据
选择元素和绑定数据是 D3 最基础的内容,本文将对其进行一个简单的介绍. 一.如何选择元素 在 D3 中,用于选择元素的函数有两个: d3.select():是选择所有指定元素的第一个 d3.sele ...
- Linux计划任务Crontab实例详解教程
说明:Crontab是Linux系统中在固定时间执行某一个程序的工具,类似于Windows系统中的任务计划程序 下面通过详细实例来说明在Linux系统中如何使用Crontab 操作系统:CentOS ...