当前位置:网站首页>【集训DAY3】石油储备计划【树形DP】
【集训DAY3】石油储备计划【树形DP】
2022-08-09 22:35:00 【VL——MOESR】
思路:
设f[i][j]表示当前i节点的子树,有j个是ave + 1的最小代价
转移方程就是 f [ x ] [ j ] = m i n ( f [ x ] [ j − k ] + f [ y ] [ k ] + a b s ( s [ y ] − ( s i z e [ y ] − k ) ∗ v a l − k ∗ ( v a l + 1 l l ) ) ∗ b [ i ] . w ) f[x][j] = min(f[x][j - k] + f[y][k] + abs(s[y] - (size[y] - k) * val - k * (val + 1ll)) * b[i].w) f[x][j]=min(f[x][j−k]+f[y][k]+abs(s[y]−(size[y]−k)∗val−k∗(val+1ll))∗b[i].w)
其中y是儿子,x是当前节点,s[y]是儿子子树的油桶数的数量,size[y]是儿子子树的大小,val是平均值,k是给儿子多少个val + 1
c o d e code code
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
ll t, val, SIZE;
ll n, sum, size[110], s[110];
ll a[110], f[110][110];
ll tot, head[110], g[110];
struct node {
ll to, next, w;
}b[10010];
void add(ll x, ll y, ll z) {
b[++ tot] = (node) {
y, head[x], z };
head[x] = tot;
}
ll min(ll x, ll y) {
return x < y ? x : y; }
void dfs(ll x, ll fa) {
size[x] = 1, s[x] = a[x];
f[x][0] = f[x][1] = 0;
for(ll i = head[x]; i; i = b[i].next) {
ll y = b[i].to;
if(fa == y) continue;
dfs(y, x);
}
for(ll i = head[x]; i; i = b[i].next) {
ll y = b[i].to;
if(y == fa) continue;
memset(g, 0x7f7f7f7f, sizeof(g));
s[x] += s[y], size[x] += size[y];
for(ll j = 0; j <= size[x] && j <= SIZE; j ++)
for(ll k = 0; k <= j && k <= size[y]; k ++)
g[j] = min(g[j], f[x][j - k] + f[y][k] + abs(s[y] - (size[y] - k) * val - k * (val + 1ll)) * b[i].w);
for(ll j = 0; j <= SIZE && j <= size[x]; j ++)
f[x][j] = g[j];
}
}
int main() {
scanf("%lld", &t);
while(t --) {
scanf("%lld", &n);
for(ll i = 1; i <= n; i ++) {
scanf("%lld", &a[i]);
sum += a[i];
}
val = sum / n, SIZE = sum % n;
for(ll i = 1; i < n; i ++) {
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
add(x, y, z), add(y, x, z);
}
memset(f, 0x7f7f7f7f, sizeof(f));
dfs(1, 0);
printf("%lld\n", f[1][SIZE]);
memset(head, 0, sizeof(head));
memset(s, 0, sizeof(s));
memset(size, 0, sizeof(size));
sum = 0;
}
return 0;
}
边栏推荐
猜你喜欢
【面试高频题】可逐步优化的链表高频题
2020年度SaaS TOP100企业名单
2022-08-09 mysql/stonedb-子查询性能提升-概论
YGG 经理人杯总决赛已圆满结束,来看看这份文字版总结!
你的手机曾经被监控过吗?
2022-08-09 mysql/stonedb-subquery performance improvement-introduction
【云原生】一文讲透Kubevela addon如何添加腾讯Crane
How to know the computer boot record?
Live Preview | ICML 2022 11 first-author scholars share online neural network, graph learning and other cutting-edge research
6款跨境电商常用工具汇总
随机推荐
2022-08-09 mysql/stonedb-subquery performance improvement-introduction
matplotlib散点图颜色分组图例
Basic operations of xlrd and xlsxwriter
测试2年,当时身边一起入行的朋友已经月薪20k了,自己还没过万,到底差在了哪里?
70. Stair Climbing Advanced Edition
多商户商城系统功能拆解24讲-平台端分销会员
【诗歌】被讨厌的勇气
关于服务治理
位图的基本原理以及应用
LiveData : Transformations.map and Transformations.switchMap usage
力扣:518. 零钱兑换 II
2022年最新《谷粒学院开发教程》:10 - 前台支付模块
技术盛宴!华云数据携六大议题亮相OpenInfra Days China
IT传奇人物菲尔德的转型经验教训及给CIO的建议
2020年度SaaS TOP100企业名单
Travel with Shengteng: See all the AI attractions in Jinling City in one day
2022-08-09 mysql/stonedb-慢SQL-Q16分析
基于 RocksDB 实现高可靠、低时延的 MQTT 数据持久化
友元类和友元函数
matplotlib散点图自定义坐标轴(文字坐标轴)