传送门

蒟蒻我原本还想着跑两边spfa,发现不行,就gg了。

首先这道题卡spfa,所以需要用堆优化的dijkstra求出最短路径

因为题目中说了,保证最短路径有且只有一条,所以可以通过dfs求出最短路径树

发现,需要给这课树加边,才能有别的路径到达一个点x

那么我们连接树上两个节点u,v,边权为w

发现,u,v到两点公共祖先的路径上的所有点(除去lca)的答案都会受到影响

且ans[i] = dis[u] + dis[v] + w - dis[i]

要使得ans最小,需要dis[u] + dis[v] + w最小,

那么直接树剖暴力修改不就好了?

另一种思路

我们可以把所有非树边取出,以dis[u] + dis[v] + w为关键字排一下,

显然,每一个点都只会求解一次,往后都不会更新答案

可以用并查集,已经更新答案的点就用并查集连接到lca,下次遇到已经更新过的点直接往上跳即可

找lca的过程和树剖类似

时间复杂度比树剖不知道高到哪里去了!

网上还有一些用左偏树或是单调队列做的,看样子好高深,蒟蒻没搞懂。。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 200001
#define heap pair<int, int> using namespace std; int n, m, cnt, tot;
int head[N], to[N << 1], val[N << 1], next[N << 1], dis[N], deep[N], ans[N], pre[N], f[N];
bool vis[N << 1];
priority_queue <heap, vector <heap>, greater <heap> > q;
vector <int> g; struct node
{
int x, y, z;
node(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}
}p[N << 1]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y, int z)
{
to[cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt++;
} inline void dijkstra()
{
int i, u, v;
memset(dis, 127, sizeof(dis));
dis[1] = 0;
q.push(make_pair(0, 1));
while(!q.empty())
{
u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
if(dis[v] > dis[u] + val[i])
{
dis[v] = dis[u] + val[i];
q.push(make_pair(dis[v], v));
}
}
}
} inline void dfs(int u, int d)
{
int i, v;
deep[u] = d;
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
if(dis[v] == dis[u] + val[i])
{
vis[i] = vis[i ^ 1] = 1;
pre[v] = u;
dfs(v, d + 1);
}
}
} inline bool cmp(node x, node y)
{
return dis[x.x] + dis[x.y] + x.z < dis[y.x] + dis[y.y] + y.z;
} inline int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
} int main()
{
int i, j, x, y, z, u, v;
n = read();
m = read();
memset(head, -1, sizeof(head));
for(i = 1; i <= m; i++)
{
x = read();
y = read();
z = read();
add(x, y, z);
add(y, x, z);
}
dijkstra();
memset(vis, 0, sizeof(vis));
dfs(1, 1);
for(u = 1; u <= n; u++)
for(i = head[u]; i ^ -1; i = next[i])
{
if(vis[i]) continue;
v = to[i];
vis[i] = vis[i ^ 1] = 1;
p[++tot] = node(u, v, val[i]);
}
sort(p + 1, p + tot + 1, cmp);
memset(ans, 127, sizeof(ans));
for(i = 1; i <= n; i++) f[i] = i;
for(i = 1; i <= tot; i++)
{
x = p[i].x;
y = p[i].y;
g.clear();
while(x ^ y)
{
if(deep[x] < deep[y]) x ^= y ^= x ^= y;
if(ans[x] <= 1e9) x = find(x);
else
{
ans[x] = dis[p[i].x] + dis[p[i].y] + p[i].z - dis[x];
g.push_back(x);
x = pre[x];
}
}
for(j = 0; j < g.size(); j++) f[g[j]] = find(x);
}
for(i = 2; i <= n; i++)
printf("%d\n", ans[i] <= 1e9 ? ans[i] : -1);
return 0;
}

  

[BZOJ1576] [Usaco2009 Jan]安全路经Travel(堆优化dijk + (并查集 || 树剖))的更多相关文章

  1. 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel

    有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...

  2. BZOJ1576: [Usaco2009 Jan]安全路经Travel(树链剖分)

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  3. BZOJ1576 [Usaco2009 Jan]安全路经Travel

    首先用Dijkstra做出最短路生成树,设dis[p]为1到p点的最短路长度 对于一条不在生成树上的边u -> v,不妨设fa为u.v的lca 则一fa到v的路径上的任意点x都可以由u达到,走的 ...

  4. BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)

    题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...

  5. 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集

    [BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...

  6. bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 665  Solved: 227[Sub ...

  7. bzoj 1576: [Usaco2009 Jan]安全路经Travel——并查集+dijkstra

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  8. BZOJ_1576_[Usaco2009 Jan]安全路经Travel&&BZOJ_3694_最短路_树链剖分+线段树

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  9. [Usaco2009 Jan]安全路经Travel BZOJ1576 Dijkstra+树链剖分+线段树

    分析: Dijkstra求最短路树,在最短路树上进行操作,详情可见上一篇博客:http://www.cnblogs.com/Winniechen/p/9042937.html 我觉得这个东西不压行写出 ...

随机推荐

  1. Python3实现自动备份

    需求 将重要文件备份到指定目录,存档文件名称为“当前日期.zip”. 前提 1) Windows系统 2) Python 3以上版本 旗舰版 #!usr/bin/python # -*- coding ...

  2. sql中的exsits和not exsits

    select * from table where exsits(sql语句) :  括号中sql语句有数据则返回这些相关id的数据集 select * from table where not ex ...

  3. MySQL索引使用等

  4. ajax上传文件以及使用中常见问题处理

    <script src="/scripts/ajaxfileupload.js"></script> <script src="/scrip ...

  5. maven打包错误:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.118 sec <<< FAILURE! - in ...

  6. XDU——受教了

    存在的问题还是很多的 GG 突然觉得刷题的目的并不是追求A.我们应该在那个过程中提高代码能力和建立模型解题能力 会的算法会巧妙应用才是王道 吐槽自己两句,写高数了

  7. eclipse中Lombok注解无效

    问题现象:eclipse中使用lombok的@Date,引用get方法时,报错. 解决方案: 在lombok官网(https://www.projectlombok.org/download)下载,或 ...

  8. python之道07

    2.用户输入一个数字,判断一个数是否是水仙花数. 水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. 那这个数就是一个水仙花数, 例如: 153 = 1******3 + 5****** ...

  9. javaEE(2)_http协议

    一.HTTP协议简介 1.客户端连上web服务器后,若想获得web服务器中的某个web资源,需遵守一定的通讯格式,HTTP协议用于定义客户端与web服务器通迅的格式.dos环境下可直接通过telnet ...

  10. 用Python写一个小爬虫吧!

    学习了一段时间的web前端,感觉有点看不清前进的方向,于是就写了一个小爬虫,爬了51job上前端相关的岗位,看看招聘方对技术方面的需求,再有针对性的学习. 我在此之前接触过Python,也写过一些小脚 ...