比较奇怪的树形背包
首先需要处理读入的问题 这题史诗递归读入
然后递归读入就不用建图
这题特点是只有叶子有价值 所以背包就不太有用
坑点就是这个人进去还得出来...
而且不能把时间都用完(导致75)
Time cost: 35min
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<queue> 6 #define ms(a,b) memset(a,b,sizeof a) 7 #define rep(i,a,n) for(int i = a;i <= n;i++) 8 #define per(i,n,a) for(int i = n;i >= a;i--) 9 #define inf 2147483647 10 using namespace std; 11 typedef long long ll; 12 typedef double D; 13 #define eps 1e-8 14 ll read() { 15 ll as = 0,fu = 1; 16 char c = getchar(); 17 while(c < ‘0‘ || c > ‘9‘) { 18 if(c == ‘-‘) fu = -1; 19 c = getchar(); 20 } 21 while(c >= ‘0‘ && c <= ‘9‘) { 22 as = as * 10 + c - ‘0‘; 23 c = getchar(); 24 } 25 return as * fu; 26 } 27 //head 28 const int N = 1005; 29 int n,V; 30 struct node { 31 int val,cst; 32 }p[N<<4]; 33 int dp[N][N]; 34 35 #define ls x<<1 36 #define rs x<<1|1 37 void input(int x) { 38 p[x].cst = read() << 1,p[x].val = read(); 39 if(!p[x].val) input(ls),input(rs); 40 } 41 42 int dfs(int x,int tot) { 43 if(!tot) return 0; 44 if(dp[x][tot]) return dp[x][tot]; 45 //sn 46 if(p[x].val) return dp[x][tot] = min(p[x].val,(tot - p[x].cst) / 5); 47 //pa 48 rep(k,0,tot - p[x].cst) 49 dp[x][tot] = max(dp[x][tot],dfs(ls,k) + dfs(rs,tot - p[x].cst - k)); 50 return dp[x][tot]; 51 } 52 53 int main() { 54 int V = read() - 1; 55 input(1); 56 printf("%d\n",dfs(1,V)); 57 return 0; 58 }
原文地址:https://www.cnblogs.com/yuyanjiaB/p/9901122.html
时间: 2024-11-08 04:46:32