标签:
9 6 3 2 2 3 2 9 3 2 4 2 5 2 3 6 2 7 2 8 2 4 3 3 3 1 1
5
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 3005; 6 const int INF = 0x3f3f3f3f; 7 int dp[maxn][maxn],cnt[maxn],val[maxn],n,m; 8 struct arc{ 9 int to,w,next; 10 arc(int x = 0,int y = 0,int z = -1){ 11 to = x; 12 w = y; 13 next = z; 14 } 15 }e[maxn<<1]; 16 int head[maxn],tot; 17 void add(int u,int v,int w){ 18 e[tot] = arc(v,w,head[u]); 19 head[u] = tot++; 20 } 21 void dfs(int u){ 22 cnt[u] = 1; 23 for(int i = 1; i <= m; ++i) 24 dp[u][i] = -INF; 25 dp[u][0] = 0; 26 if(head[u] == -1){ 27 dp[u][1] = val[u]; 28 return; 29 } 30 for(int i = head[u]; ~i; i = e[i].next){ 31 dfs(e[i].to); 32 cnt[u] += cnt[e[i].to]; 33 for(int j = cnt[u]; j >= 1; --j){ 34 for(int k = 0; k < j; ++k) 35 dp[u][j] = max(dp[u][j],dp[u][k]+dp[e[i].to][j-k]-e[i].w); 36 } 37 } 38 } 39 int main(){ 40 int v,w,k; 41 while(~scanf("%d %d",&n,&m)){ 42 memset(head,-1,sizeof head); 43 for(int i = 1; i <= n - m; ++i){ 44 scanf("%d",&k); 45 while(k--){ 46 scanf("%d %d",&v,&w); 47 add(i,v,w); 48 } 49 } 50 for(int i = n - m + 1; i <= n; ++i) 51 scanf("%d",val+i); 52 dfs(1); 53 int ans = 0; 54 for(int i = m; i >= 1; --i) 55 if(dp[1][i] >= 0) { 56 ans = i; 57 break; 58 } 59 printf("%d\n",ans); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4403645.html