luogu P1270 "訪問"美術館 樹dp

來源:互聯網
上載者:User

標籤:org   efi   open   none   時間   splay   ons   https   isp   

傳送門

比較奇怪的樹形背包

首先需要處理讀入的問題 這題史詩遞迴讀入

然後遞迴讀入就不用建圖

這題特點是只有葉子有價值 所以背包就不太有用

坑點就是這個人進去還得出來...

而且不能把時間都用完(導致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 214748364710 using namespace std;11 typedef long long ll;12 typedef double D;13 #define eps 1e-814 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 //head28 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<<136 #define rs x<<1|137 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     //sn46     if(p[x].val) return dp[x][tot] = min(p[x].val,(tot - p[x].cst) / 5);47     //pa48     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 }
View Code

 

luogu P1270 "訪問"美術館 樹dp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.