题解

这题很明显发现一个点到另一个点,必然最多只有一个进入下界的点和一个出来的点

分类讨论入点和出点的位置

要么都在 \(u->lca\) 或都在 \(lca->v\) 或分别有一个

那就有一个倍增做法

维护最优入点和最优出点即可

但考场并没想到这种方法

反而只想到了没脑的倍增维护矩阵转移的方法

我们设 \(f_{u,0/1}\) 表示从它的儿子 \(v\) 过来的最优答案

把转移式子写成矩阵的形式,倍增维护即可

但我的常数太大了,沦为和暴力老哥同分

\(Code\)

#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC diagnostic error "-std=c++14"
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC optimize("fast-math","unroll-loops","no-stack-protector","inline")
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std; const int N = 2e5 + 5;
int n;
LL a[N]; inline void read(int &x)
{
x = 0; int f = 1; char ch = getchar();
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();
x *= f;
}
inline void read(LL &x)
{
x = 0; int f = 1; char ch = getchar();
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();
x *= f;
} int h[N], tot;
struct edge{int to, nxt; LL w;}e[N << 1];
inline void add(int x, int y, LL z){e[++tot] = edge{y, h[x], z}, h[x] = tot;} const LL INF = 1e18;
struct Matrix{
LL a[3][3];
inline Matrix()
{
for(register int i = 0; i < 3; i++)
for(register int j = 0; j < 3; j++) a[i][j] = INF;
}
inline Matrix operator * (const Matrix &b)
{
Matrix c;
for(register int i = 0; i < 3; i++)
for(register int j = 0; j < 3; j++)
for(register int k = 0; k < 3; k++)
c.a[i][j] = min(c.a[i][j], a[i][k] + b.a[k][j]);
return c;
}
}f[N][20];
inline Matrix I()
{
Matrix c;
c.a[0][0] = c.a[1][1] = c.a[2][2] = 0;
return c;
}
inline Matrix BOT(int u)
{
Matrix c;
c.a[0][0] = c.a[1][0] = 0, c.a[2][0] = a[u];
return c;
}
inline Matrix Node(int u, int v, LL w)
{
Matrix c;
c.a[0][0] = 8 * w, c.a[0][1] = w + a[u] + a[v], c.a[0][2] = a[u] + w;
c.a[1][0] = 8 * w, c.a[1][1] = w + a[u] + a[v], c.a[1][2] = a[u] + w;
c.a[2][0] = 8 * w + a[u], c.a[2][1] = w + a[v], c.a[2][2] = w;
return c;
} int dep[N], anc[N][20], d[N];
void bfs()
{
int head = 0, tail = 1;
d[1] = 1;
while (head < tail)
{
int x = d[++head];
for(register int i = 1; i <= 18; i++)
if (anc[x][i - 1]) anc[x][i] = anc[anc[x][i - 1]][i - 1], f[x][i] = f[anc[x][i - 1]][i - 1] * f[x][i - 1];
else break;
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == anc[x][0]) continue;
anc[v][0] = x, f[v][0] = Node(x, v, e[i].w);
dep[v] = dep[x] + 1, d[++tail] = v;
}
}
}
inline LL getans(int x, int y)
{
if (dep[x] < dep[y]) swap(x, y);
int deep = dep[x] - dep[y];
Matrix retx = I(), rety = I(), Bx = BOT(x), By = BOT(y);
for(register int i = 18; i >= 0; i--)
if ((deep >> i) & 1) retx = f[x][i] * retx , x = anc[x][i];
if (x == y)
{
retx = retx * Bx;
return min(retx.a[0][0], retx.a[2][0] + a[x]);
}
for(register int i = 18; i >= 0; i--)
if (anc[x][i] ^ anc[y][i])
{
retx = f[x][i] * retx , rety = f[y][i] * rety;
x = anc[x][i], y = anc[y][i];
}
retx = f[x][0] * retx * Bx, rety = f[y][0] * rety * By;
return min(min(retx.a[0][0] + rety.a[0][0], retx.a[2][0] + rety.a[2][0]),
min(retx.a[0][0] + rety.a[2][0] + a[anc[x][0]], retx.a[2][0] + rety.a[0][0] + a[anc[x][0]]));
} int main()
{
freopen("minecraft.in", "r", stdin);
freopen("minecraft.out", "w", stdout);
read(n);
for(register int i = 1; i <= n; i++) read(a[i]);
int x, y; LL z;
for(register int i = 1; i < n; i++) read(x), read(y), read(z), add(x, y, z), add(y, x, z);
bfs();
int q; read(q);
for(; q; --q) read(x), read(y), printf("%lld\n", getans(x, y));
}

最新文章

  1. python 替换 文件夹下的 文件名称 及 文件内容
  2. VMWare克隆之后设置eth0
  3. c# 操作datatable
  4. 有趣的hello word
  5. jwplayer 源代码重新编译
  6. AdHoc发布时出现重复Provisioning Profile的解决方案
  7. OC和Swift中的UITabBar和UINaviGationBar的适配 [UITabbar在IPad中的适配]
  8. 机器学习排序算法:RankNet to LambdaRank to LambdaMART
  9. navicate 远程无法链接linux上mysql数据库问题
  10. 【2018.04.27 C与C++基础】关于switch-case及if-else的效率问题
  11. Linux命令集
  12. 让rpc支持双向通信
  13. Windows cordova build Error: Could not find gradle wrapper within Android SDK.(转)
  14. Python自动化开发 - 模块与包
  15. JS实现千分位
  16. 转:FSMT:文件服务器从03迁移到08R2实战演练
  17. CentOS7忘记root密码的解决方法
  18. shell学习笔记之命令(四)
  19. web 服务基础
  20. centos下如何停止ping命令

热门文章

  1. css初始化收集
  2. JavaEE Day01 基础加强
  3. 【中间件】K8S-kubernetes
  4. bug处理记录:Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512M; support was removed in 8.0
  5. Java的两大、三类代理模式
  6. day07-功能实现06
  7. [Codeforces Round #841]
  8. VSCode运行C/C++配置
  9. P3845 [TJOI2007]球赛
  10. oracle创建全文索引(oracle text)