题解

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

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

要么都在 \(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));
}

JZOJ 2474. 【GDKOI 2021普及组DAY2】我的世界的更多相关文章

  1. [GDKOI2021] 普及组 Day2 总结

    [ G D K O I 2021 ] 普 及 组 D a y 2 总 结 [GDKOI2021] 普及组 Day2 总结 [GDKOI2021]普及组Day2总结 时间安排和昨天的GDKOI2021 ...

  2. JZOJ.2117. 【2016-12-30普及组模拟】台风

    题目大意: 天气预报频道每天从卫星上接受卫星云图.图片被看作是一个矩阵,每个位置上要么是”#”,要么”.”,”#”表示该位置没有云,”.”表示有云,地图上每个位置有多达8个相邻位置,分别是,左上.上. ...

  3. GDOI 2021 普及组溺水记

    Day 1 T1 一看样例:答案不就是 \(\dfrac{\max_{i=1}^n a_i +1}{2}\) 吗? 于是自信打上,拍都不拍.然后就,,对了? 插曲:自己出了一个极端数据,发现 scan ...

  4. [GDKOI2021] 普及组 Day3 总结 && 题解

    [ G D K O I 2021 ] 普 及 组 D a y 3 总 结 时间安排和昨天的GDKOI2021 Day2一样. 早上四个小时的快乐码题时间,然鹅我打了半小时的表 然后就是下午的题目讲解和 ...

  5. NOIP2017普及组初赛试题及答案

    普及组C++语言试题 一.单项选择题(共 20 题,每题 1.5 分,共计 30 分:每题有且仅有一个正确选项) 1.在 8 位二进制补码中,10101011 表示的数是十进制下的( ). A. 43 ...

  6. noip2017爆炸记——题解&总结&反省(普及组+提高组)

    相关链接: noip2018总结 noip2017是我见过的有史以来最坑爹的一场考试了. 今年北京市考点有一个是我们学校,我还恰好被分到了自己学校(还是自己天天上课的那个教室),于是我同时报了普及提高 ...

  7. 2016.8.15上午纪中初中部NOIP普及组比赛

    2016.8.15上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1333 这次比赛不怎么好,因为这套题目我并不是很擅长. 可同学们 ...

  8. 2016.9.10初中部上午NOIP普及组比赛总结

    2016.9.10初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1340 好不爽!翻车了!不过排名差不多在中间偏上一点, 还好不是 ...

  9. 2016.9.3初中部上午NOIP普及组比赛总结

    2016.9.3初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1339 这次真爽,拿了个第四!(我还被班主任叫过去1小时呢!) 进 ...

  10. 2016.8.19上午初中部NOIP普及组比赛总结

    2016.8.19上午初中部NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1338 这次总结发得有点晚啊!我在这里解释一下, 因为浏览器的问 ...

随机推荐

  1. <一>对象使用过程中背后调用了哪些方法

    代码1 #include <iostream> using namepspace std; class Test { public: Test(int a=10):ma(a){cout&l ...

  2. nginx压力测试及限速

    测试工具:Apache ab windows安装教程:https://www.cnblogs.com/laijinquan/p/14694655.html 64位下载地址:https://www.ap ...

  3. 学生管理系统Python

    student1=[ {1:'lucy','age':17,'sex':'n','Pnum':1111111}, {2:'tom','age':17,'sex':'m','Pnum':2222222} ...

  4. python 中文分词工具

    python 中文分词工具 jieba,https://github.com/fxsjy/jieba jieba_fast,https://github.com/deepcs233/jieba_fas ...

  5. SQL注入漏洞原理与利用

    SQL注入漏洞原理与利用 SQL注入漏洞流程 判断注入类型 判断字段个数 查询回显位 查询数据库名 查询表.字段名 查询内容 判断注入类型 1.数字型注入点判断 当要输入的参数x为数字型时,后端脚本中 ...

  6. 异构混排在vivo互联网的技术实践

    作者:vivo 互联网算法团队- Shen Jiyi 本文根据沈技毅老师在"2022 vivo开发者大会"现场演讲内容整理而成. 混排层负责将多个异构队列的结果如广告.游戏.自然量 ...

  7. js 获取当前时间转换时间戳 (毫秒)

    js 当前时间转换毫秒数 五种方式   var date = new Date().getTime(); var date = new Date().valueOf(); var date = +ne ...

  8. element-ui中table组件的表格嵌套Select,table中使用select

    在table组件中,有一个<template slot-scope="scope"></template>,这个模板有一个slot-scope属性,这个属性 ...

  9. vulnhub靶场之GROTESQUE: 3.0.1

    准备: 攻击机:虚拟机kali.本机win10. 靶机:Grotesque: 3.0.1,下载地址:https://download.vulnhub.com/grotesque/grotesque3. ...

  10. 【转载】EXCEL VBA 关于范围选择代码集

    Range("A1:B2").Select '选中"A1"."A2"."B1"."B2"四个连续的单 ...