BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)
题意
给你一张无向图,保证从1号点到每个点的最短路唯一。对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度
Sol
emmm,考场上想了个贪心开心的飞起然而只多得了10分qwq
正解比较神仙。
首先把最短路树建出来,考虑一条非树边$(u, v)$什么时候能更新答案
结论是:除了他们的LCA外的子树内其他都可以更新,且新的权值为$dis[u] + dis[v] + w(u, v) - dis[x]$,$x$表示新节点
这样我们把所有的边按照$dis[u] + dis[v] + w(u, v)$排序,显然,一个点如果被更新过那么就再也不会被更新了。
用并查集把已经更新过的点缩起来即可
这题的关键是要发现非树边与答案之间的性质。。
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
const int MAXN = 1e6 + , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M;
vector<Pair> v[MAXN];
struct Edge {
int u, v, w;
}E[MAXN];
int num = ;
int dis[MAXN], top[MAXN], vis[MAXN], cnt = , ans[MAXN];
void Dij() {
memset(dis, 0x7f, sizeof(dis));
dis[] = ;
priority_queue<Pair> q; q.push(MP(, ));
while(!q.empty()) {
int p = q.top().se; q.pop();
if(vis[p]) continue; vis[p] = ;
for(int i = ; i < v[p].size(); i++) {
int to = v[p][i].fi, w = v[p][i].se;
if(dis[to] > dis[p] + w && (!vis[to])) {
top[to] = p;
dis[to] = dis[p] + w;
q.push(MP(-dis[to], to));
}
}
}
}
int comp(const Edge &a, const Edge &b) {
return dis[a.u] + dis[a.v] + a.w < dis[b.u] + dis[b.v] + b.w;
}
int fa[MAXN];
int unionn(int x, int y) {
fa[x] = y;
}
int Find(int x) {
if(fa[x] == x) return fa[x];
else return fa[x] = Find(fa[x]);
}
int solve(int x, int y, int w) {
while((x = Find(x)) != (y = Find(y))) {
//int dx = dis[x], dy = dis[y];
if(dis[x] < dis[y]) swap(x, y);
ans[x] = w - dis[x];
x = (fa[x] = top[x]);
cnt++;
}
}
int main() {
N = read(); M = read();
for(int i = ; i <= M; i++) {
int x = read(), y = read(), z = read();
v[x].push_back(MP(y, z));
v[y].push_back(MP(x, z));
}
Dij();
for(int i = ; i <= N; i++) {
fa[i] = i;
for(int j = ; j < v[i].size(); j++) {
int to = v[i][j].fi, w = v[i][j].se;
if(top[to] == i || top[i] == to) continue;
E[++num] = (Edge) {i, to, w};
}
}
sort(E + , E + num + , comp);
for(int i = ; i <= num; i++) {
solve(E[i].u, E[i].v, dis[E[i].u] + dis[E[i].v] + E[i].w);
if(cnt == N - ) break;
}
for(int i = ; i <= N; i++)
printf("%d\n", ans[i] ? ans[i] : -);
return ;
}
BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)的更多相关文章
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
[BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...
- BZOJ.1576.[Usaco2009 Jan]安全路经Travel(树形DP 并查集)
题目链接 BZOJ 洛谷 先求最短路树.考虑每一条非树边(u,v,len),设w=LCA(u,v),这条边会对w->v上的点x(x!=w)有dis[u]+dis[v]-dis[x]+len的距离 ...
- 【思维题 并查集 图论】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
首先用Dijkstra做出最短路生成树,设dis[p]为1到p点的最短路长度 对于一条不在生成树上的边u -> v,不妨设fa为u.v的lca 则一fa到v的路径上的任意点x都可以由u达到,走的 ...
- 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(堆优化dijk + (并查集 || 树剖))
传送门 蒟蒻我原本还想着跑两边spfa,发现不行,就gg了. 首先这道题卡spfa,所以需要用堆优化的dijkstra求出最短路径 因为题目中说了,保证最短路径有且只有一条,所以可以通过dfs求出最短 ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MB Submit: 665 Solved: 227[Sub ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel——并查集+dijkstra
Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...
- BZOJ 1576: [Usaco2009 Jan]安全路经Travel
日常自闭半小时后看题解,太弱了qwq. 感觉这道题还是比较难的,解法十分巧妙,不容易想到. 首先题目说了起点到每个点的最短路都是唯一的,那么对这个图求最短路图必定是一棵树,而且这棵树是唯一的. 那么我 ...
随机推荐
- df 参数说明
df -h 由 df 命令显示出的各列信息的含义分别是: Filesystem:表示该文件系统位于哪个分区,因此该列显示的是设备名称: 1K-blocks:此列表示文件系统的总大小,默认以 KB 为单 ...
- ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)
传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...
- 转 Mindoc搭建流程 文档多人编辑工具。
安装方法参考: https://www.yuanmas.com/info/1bz9Y126zx.html https://www.iminho.me/version.html #step 1,安装My ...
- 三节点Hadoop集群搭建
1. 基础环境搭建 新建3个CentOS6.5操作系统的虚拟机,命名(可自定)为masternode.slavenode1和slavenode2.该过程参考上一篇博文CentOS6.5安装配置详解 2 ...
- javaweb项目导入myecplise出错
项目导入出错:移动项目的时候.classpath .project不要删 build path -> use for source floder 把某一文件夹当作源码文件夹 bulid path ...
- log(A^B) = BlogA
令 x = logA, y = logB, z=log(AB) .2x = A, 2y = B, 2z = AB, 则有 2z = AB = (2x)^(2y) = 2x(2^y) ,有z = x*2 ...
- SpringBoot | 第九章:Mybatis-plus的集成和使用
前言 本章节开始介绍数据访问方面的相关知识点.对于后端开发者而言,和数据库打交道是每天都在进行的,所以一个好用的ORM框架是很有必要的.目前,绝大部分公司都选择MyBatis框架作为底层数据库持久化框 ...
- 如何 求Ifeature 的面积
IArea pArea = pfteature_Source.Shape as IArea;// IArea来自geometry double dArea = Math.Round(pArea.Ar ...
- Cucumber 步骤中传Data Table作为参数
引用链接:http://cukes.info/step-definitions.html Data Tables Data Tables are handy for specifying a larg ...
- ArcMap中提取影像数据边界
1.前言 客户手里有一些经过裁剪的不规则多边形影像数据(如图例所示),希望能批量获取该类影像的边界信息,即影像对应的面信息,边界线信息.这里我们提供一种利用镶嵌数据集Footprint图层的方法来获取 ...