当前位置:网站首页>[noi Simulation Competition] send (tree DP)
[noi Simulation Competition] send (tree DP)
2022-06-24 08:59:00 【DD(XYX)】
Topic


Answer key
Finding the nearest transfer station can be directly transformed into matching any transfer station .
Tree form DP, There's nothing to say about this .
The main thing is how to design the state .
The positive solution and the state designed by most people are very , So you don't need anything else to go through .
But I designed a bad one DP:
Make d p 1 [ i ] [ j ] dp_1[i][j] dp1[i][j] It means that i i i In the subtree , There are j j j Keys unowned , The minimum value of the sum of all edge contributions in the subtree . d p 2 [ i ] [ j ] dp_2[i][j] dp2[i][j] It means that i i i subtree Outside All in all j j j Three key points should be connected , subtree Inside The minimum value of the sum of all edge contributions .
Merge two subtrees x , y x,y x,y when , Two DP You can carry them separately , besides , d p 2 [ x ] [ i ] dp_2[x][i] dp2[x][i] Can also from d p 2 [ x / y ] [ i + j ] + d p 1 [ y / x ] [ j ] + . . . dp_2[x/y][i+j]+dp_1[y/x][j]+... dp2[x/y][i+j]+dp1[y/x][j]+... Turn around . Greedy thought , We just need external needs to swallow up ownerless needs , At this time, the key points of ownerlessness must not be added . Finally, we will discuss how to set up a transfer station at the root of the subtree .
But there's a small problem : The number of states of each subtree is equal to O ( n ) O(n) O(n) Of , although d p 1 dp_1 dp1 The complexity of can be guaranteed to be O ( n 2 ) O(n^2) O(n2) , however d p 2 dp_2 dp2 yes O ( n 3 ) O(n^3) O(n3) Of . Use the greedy technique again : The total demand outside the subtree will not exceed the total number of key points in the subtree , Otherwise, the transfer station can be moved up .
Time complexity O ( n 2 ) O(n^2) O(n2) .
CODE
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<random>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#pragma GCC optimize(2)
using namespace std;
#define MAXN 3005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x))
#define FI first
#define SE second
#define PR pair<int,int>
int xchar() {
static const int maxn = 1000000;
static char b[maxn];
static int pos = 0,len = 0;
if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
if(pos == len) return -1;
return b[pos ++];
}
// #define getchar() xchar()
LL read() {
LL f = 1,x = 0;int s = getchar();
while(s < '0' || s > '9') {
if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
while(s >= '0' && s <= '9') {
x = (x<<1) + (x<<3) + (s^48);s = getchar();}
return f*x;
}
void putpos(LL x) {
if(!x)return ;putpos(x/10);putchar((x%10)^48);}
void putnum(LL x) {
if(!x) {
putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
void AIput(LL x,int c) {
putnum(x);putchar(c);}
int n,m,s,o,k;
int hd[MAXN],nx[MAXN<<1],v[MAXN<<1],w[MAXN<<1],cne;
void ins(int x,int y,int z) {
nx[++cne] = hd[x]; v[cne] = y; hd[x] = cne; w[cne] = z;
}
LL dp[MAXN][MAXN],dp2[MAXN][MAXN],td[MAXN],td2[MAXN];
int siz[MAXN],ct[MAXN],C;
void dfs(int x,int ff) {
siz[x] = ct[x];
for(int i = 0;i <= n;i ++) dp[x][i] = 1e18,dp2[x][i] = 1e18;
dp[x][0] = C; dp[x][ct[x]] = 0;
for(int i = 0;i <= ct[x];i ++) dp2[x][i] = C;
for(int ii = hd[x];ii;ii = nx[ii]) {
int y = v[ii],W = w[ii]; if(y == ff) continue;
dfs(y,x);
for(int i = siz[x];i >= 0;i --) {
td[i] = dp[x][i],td2[i] = dp2[x][i];
dp[x][i] = 1e18; dp2[x][i] = 1e18;
}
for(int i = siz[x];i >= 0;i --) {
for(int j = siz[y];j >= 0;j --) {
dp[x][i+j] = min(dp[x][i+j],td[i] + dp[y][j] + W*1ll*j);
dp2[x][i+j] = min(dp2[x][i+j],td2[i] + dp2[y][j] + W*1ll*j);
}
for(int j = 0;j <= siz[y] && j <= i;j ++) {
dp2[x][i-j] = min(dp2[x][i-j],td2[i] + dp[y][j] + W*1ll*j);
}
}
for(int i = siz[y];i >= 0;i --) {
for(int j = 0;j <= siz[x] && j <= i;j ++) {
dp2[x][i-j] = min(dp2[x][i-j],dp2[y][i] + W*1ll*i + td[j]);
}
}
siz[x] += siz[y];
}
LL mn = 1e18;
for(int i = 0;i <= siz[x];i ++) mn = min(mn,dp[x][i] + C);
dp2[x][0] = dp[x][0] = min(min(dp2[x][0],dp[x][0]),mn);
for(int i = 1;i <= siz[x];i ++) dp2[x][i] = min(dp2[x][i],mn);
return ;
}
int main() {
freopen("post.in","r",stdin);
freopen("post.out","w",stdout);
n = read(); m = read(); C = read();
for(int i = 1;i < n;i ++) {
s = read();o = read();k = read();
ins(s,o,k); ins(o,s,k);
}
for(int i = 1;i <= m;i ++) {
ct[read()] ++;
}
dfs(1,0);
AIput(dp[1][0],'\n');
return 0;
}
边栏推荐
- tcpdump抓包实现过程
- Opencv maximum filtering (not limited to images)
- Floating error waiting for changelog lock
- PM2 deploy nuxt3 JS project
- OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
- Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
- [pytoch basic tutorial 31] youtubednn model analysis
- 数据中台:中台实践与总结
- 【PyTorch基础教程30】DSSM双塔模型代码解析
- 剑指 Offer 55 - I. 二叉树的深度-dfs法
猜你喜欢
![[noi Simulation Competition] geiguo and time chicken (structure)](/img/4c/ed1b5bc2bed653c49b8b7922ce1674.png)
[noi Simulation Competition] geiguo and time chicken (structure)

4274. suffix expression

【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理

数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题

【牛客】HJ1 字符串最后一个单词的长度

Data middle office: middle office architecture and overview

原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同

什么是图神经网络?图神经网络有什么用?

数据中台:中台架构及概述

China chip Unicorn Corporation
随机推荐
剑指 Offer 55 - I. 二叉树的深度-dfs法
Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in thi
110. balanced binary tree recursive method
What is the future development trend of Business Intelligence BI
I heard that you are still spending money to buy ppt templates from the Internet?
1844. replace all numbers with characters
How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
IDEA另起一行快捷键
[noi Simulation Competition] geiguo and time chicken (structure)
数据中台:数据采集和抽取的技术栈详解
Pymysql inserts data into MySQL and reports an error for no reason
Matlab camera calibrator camera calibration
The form image uploaded in chorme cannot view the binary image information of the request body
用VNC Viewer的方式远程连接无需显示屏的树莓派
Qingcloud based "real estate integration" cloud solution
Data middle office: middle office practice and summary
Installation of sophus package in slam14 lecture
216. 组合总和 III-枚举法
基于单片机开发的酒精浓度测试仪方案
SLAM14讲中Sophus包的安装问题