当前位置:网站首页>【集训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;
}
边栏推荐
猜你喜欢
新增一地公布2022下半年软考报考时间
你的手机曾经被监控过吗?
技术盛宴!华云数据携六大议题亮相OpenInfra Days China
[Interface Test] Decoding the request body string of the requests library
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
全面解析FPGA基础知识
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
MVC与MVVM模式的区别
为什么刀具数据库无法打开?
Gumbel distribution of discrete choice model
随机推荐
Redis集群
MQTT X Web:在线的 MQTT 5.0 客户端工具
【mysql】查询今天9点
A Shanghai technology company was fined 220,000 for brushing orders, exposing the gray industry chain of online brushing
力扣:322. 零钱兑换
Leetcode 701. 二叉搜索树中的插入操作
matplotlib散点图自定义坐标轴(文字坐标轴)
离散选择模型之Gumbel分布
Controller层代码这么写,简洁又优雅!
什么是平面文件数据库? 如何导入多种格式的文件:DSV、JSON、XML?
Leetcode 235. 二叉搜索树的最近公共祖先
Force Buckle: 474. Ones and zeros
全面解析FPGA基础知识
多商户商城系统功能拆解25讲-平台端分销申请
CAD 绘制圆角处理
关于服务治理
Click: 377. Combined Sum Ⅳ
完全背包理论
matplotlib散点图颜色分组图例
tiup cluster start