同 bzoj3694

需要先求出最短路树

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue> const int N = 4e5 + , M = 4e5 + ; using namespace std; struct Node {
int u, v, w, nxt;
} G[M << ], E[M << ];
int n, m;
int head[N], now, js, dis[N]; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void write_int(int x) {
printf("%d\n", x);
} inline void Add(int u, int v, int w) {
G[++ now].v = v, G[now].w = w, G[now].nxt = head[u], head[u] = now;
} int fa[N], deep[N], topp[N], size[N], son[N], tree[N], Tree; void Dfs_1(int u, int f_, int dep) {
fa[u] = f_, deep[u] = dep, size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v == f_) continue;
dis[v] = dis[u] + G[i].w;
Dfs_1(v, u, dep + );
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
} void Dfs_2(int u, int tp) {
topp[u] = tp, tree[u] = ++ Tree;
if(!son[u]) return ;
Dfs_2(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt)
if(G[i].v != fa[u] && G[i].v != son[u]) Dfs_2(G[i].v, G[i].v);
} const int oo = ;
int Minn[N << ]; #define lson jd << 1
#define rson jd << 1 | 1 void Build_tree(int l, int r, int jd) {
Minn[jd] = oo;
if(l == r) return ;
int mid = (l + r) >> ;
Build_tree(l, mid, lson), Build_tree(mid + , r, rson);
} void Sec_G(int l, int r, int jd, int x, int y, int w) {
if(x <= l && r <= y) {
Minn[jd] = std:: min(Minn[jd], w);
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, w);
if(y > mid) Sec_G(mid + , r, rson, x, y, w);
} void Sec_G_imp(int x, int y, int w) {
int tpx = topp[x], tpy = topp[y];
while(tpx != tpy) {
if(deep[tpx] < deep[tpy]) std:: swap(tpx, tpy), std:: swap(x, y);
Sec_G(, n, , tree[tpx], tree[x], w);
x = fa[tpx], tpx = topp[x];
}
if(x == y) return ;
if(deep[x] < deep[y]) std:: swap(x, y);
Sec_G(, n, , tree[y] + , tree[x], w);
} int Ans[N]; void Dfs_tree(int l, int r, int jd) {
if(l == r) {
Ans[l] = Minn[jd];
return ;
}
int mid = (l + r) >> ;
Minn[lson] = std:: min(Minn[lson], Minn[jd]);
Minn[rson] = std:: min(Minn[rson], Minn[jd]);
Dfs_tree(l, mid, lson), Dfs_tree(mid + , r, rson);
} int shead[N];
struct Node_2 {
int u, v, w, id, nxt;
} sG[M << ], sE[M << ];
int stot = ;
bool sOk[N];
queue <int> sQ;
int sdis[N];
bool svis[N];
int sto[N]; struct Short { void Link(int u, int v, int w, int id) {
sG[++ stot].v = v, sG[stot].id = id, sG[stot].w = w, sG[stot].nxt = shead[u], shead[u] = stot;
} void Spfa(int S) {
for(int i = ; i <= n; i ++) sdis[i] = oo;
sdis[S] = ;
sQ.push(S);
while(!sQ.empty()) {
int stop = sQ.front();
sQ.pop();
svis[stop] = ;
for(int i = shead[stop]; ~ i; i = sG[i].nxt) {
int v = sG[i].v;
if(sdis[v] > sdis[stop] + sG[i].w) {
sdis[v] = sdis[stop] + sG[i].w;
sOk[sto[v]] = ;
sto[v] = sG[i].id;
sOk[sto[v]] = ;
if(svis[v] == ) {
sQ.push(v);
}
}
}
}
} void Work() {
for(int i = ; i <= n; i ++) shead[i] = -;
for(int i = ; i <= m; i ++) {
int u = sE[i].u, v = sE[i].v, w = sE[i].w, id = sE[i].id;
Link(u, v, w, id), Link(v, u, w, id);
}
Spfa();
}
}AB; int main() {
n = read(), m = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i <= m; i ++) {
int u = read(), v = read(), w = read();
sE[i].u = u, sE[i].v = v; sE[i].w = w; sE[i].id = i;
}
if(Judge()) { return ;
}
AB.Work();
int tot = ;
for(int i = ; i <= m; i ++) {
int u = sE[i].u, v = sE[i].v, w = sE[i].w; // id = sE[i].id;
if(sOk[i] == ) {
Add(u, v, w), Add(v, u, w);
} else {
E[++ tot].u = u, E[tot].v = v; E[tot].w = w;
}
}
Dfs_1(, , );
Dfs_2(, );
Build_tree(, n, );
for(int i = ; i <= tot; i ++) {
int x = E[i].u, y = E[i].v;
Sec_G_imp(x, y, dis[x] + dis[y] + E[i].w);
}
Dfs_tree(, n, );
for(int i = ; i <= n; i ++) {
if(Ans[tree[i]] == oo) write_int(-);
else write_int(Ans[tree[i]] - dis[i]);
}
return ;
}

luogu 2934的更多相关文章

  1. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  2. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  3. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  4. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  5. Luogu 考前模拟Round. 1

    A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...

  6. luogu P2580 于是他错误的点名开始了

    luogu  P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...

  7. CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)

    CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...

  8. Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)

    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...

  9. Luogu 1962 斐波那契数列(矩阵,递推)

    Luogu 1962 斐波那契数列(矩阵,递推) Description 大家都知道,斐波那契数列是满足如下性质的一个数列: f(1) = 1 f(2) = 1 f(n) = f(n-1) + f(n ...

随机推荐

  1. Yii2 redis 使用方法

    /** * 基于 yii2.0 redis使用方法 *///项目根目录命令行执行 composer require --prefer-dist yiisoft/yii2-redis; //在配置文件中 ...

  2. php-获取某个文件夹下面的文件数量

    /** * 获取文件夹下文件的数量 * @param $url 传入一个url如:/apps/web * @return int 返回文件数量 */ public function getFileNu ...

  3. Javascritp Array数组方法总结

    合并数组 - concat() 用法一 (合并两个数组) var hege = ["Cecilie", "Lone"]; var stale = [" ...

  4. 怎样重启ssh服务

    尝试下面两个命令: service sshd restart systemctl restart sshd.service

  5. 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)

    原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...

  6. Powershell学习笔记:(一)、初识Powershell

    什么是Powershell? MSDN上的说明是:PowerShell 是构建于 .NET 上基于任务的命令行 shell 和脚本语言. PowerShell 可帮助系统管理员和高级用户快速自动执行用 ...

  7. Python之Flask

    一.Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 .Flask使用 BSD 授权. Flask是一个轻 ...

  8. Java 之 IO 异常的处理【了解】

    一.JDK7 前的处理 前面的 Demo 中,一直把异常抛出,而在实际中并不能这样处理,建议使用 try...catch...finally 代码块,处理异常部分. 格式: try{ 可能会产出异常的 ...

  9. HBase分布式搭建常见错误

    [root@node001 bin]# hbase shell SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found bin ...

  10. Linux之mysql的安装与,主从设置

    一,MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...