POJ 1155 TELE

TELE

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 1155
64-bit integer IO format: %lld      Java class name: Main
A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tree is a transmitter that emits the football match, the leaves of the tree are the potential users and other vertices in the tree are relays (transmitters). 
The price of transmission of a signal from one transmitter to another or to the user is given. A price of the entire broadcast is the sum of prices of all individual signal transmissions. 
Every user is ready to pay a certain amount of money to watch the match and the TV-network then decides whether or not to provide the user with the signal. 
Write a program that will find the maximal number of users able to watch the match so that the TV-network's doesn't lose money from broadcasting the match.
 

Input

The first line of the input file contains two integers N and M, 2 <= N <= 3000, 1 <= M <= N-1, the number of vertices in the tree and the number of potential users. 
The root of the tree is marked with the number 1, while other transmitters are numbered 2 to N-M and potential users are numbered N-M+1 to N. 
The following N-M lines contain data about the transmitters in the following form: 
K A1 C1 A2 C2 ... AK CK 
Means that a transmitter transmits the signal to K transmitters or users, every one of them described by the pair of numbers A and C, the transmitter or user's number and the cost of transmitting the signal to them. 
The last line contains the data about users, containing M integers representing respectively the price every one of them is willing to pay to watch the match.
 

Output

The first and the only line of the output file should contain the maximal number of users described in the above text.
 

Sample Input

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

Sample Output

5

Source

 
解题:树形dp,dp[u][j]表示以u为根的子树给j个顾客服务
 
 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 }
View Code

 

posted @ 2015-04-08 21:06  狂徒归来  阅读(213)  评论(0编辑  收藏  举报