1. 程式人生 > >【樹形dp】TELE

【樹形dp】TELE

lines ext 找到 mat orm money them spa number

[POJ1155]TELE
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5376 Accepted: 2973

Description

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

Croatia OI 2002 Final Exam - Second Day 題目大意:有一個電視網絡,每條邊都有邊權,每個人都願意付一定的價格來收看電視,問電視臺在不虧本的情況下最多能滿足多少個用戶? 試題分析:設dp[i][j]表示i號節點選j個用戶賺的錢。      那麽dp[i][j]=max(dp[i][j],dp[i->son][t]+dp[i][j-t]-Cost[i->son]*2)      邊界條件(葉子結點):dp[i][1]=val[i],dp[i][0]=0;       最後從大到小枚舉人數,找到第一個dp[1][ans]為正的即可
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;
	return x*f;
}
const int MAXN=8001;
const int INF=999999;
int N,M;
int Node[MAXN],Root[MAXN],Next[MAXN],Cost[MAXN];
int cnt;
int val[MAXN];
int tel[MAXN];
int dp[3001][3001];

void addedge(int u,int v,int w){
	++cnt;
	Node[cnt]=v; Cost[cnt]=w;
	Next[cnt]=Root[u];
	Root[u]=cnt;
	return ;
}
void dfs(int x){
	if(x>N-M){
		tel[x]=1;
		dp[x][1]=val[x];
		dp[x][0]=0;
		return ;
	}
	for(int k=Root[x];k;k=Next[k]){
		int son=Node[k];
		dfs(son);
	}
	int tmp=0;dp[x][0]=0;
	for(int k=Root[x];k;k=Next[k]){
		int son=Node[k];
		tmp+=tel[son];
		for(int t=tmp;t>=1;t--){
			for(int p=1;p<=tel[son];p++){
			    if(p>t) break;
			    dp[x][t]=max(dp[x][t],dp[son][p]+dp[x][t-p]-Cost[k]);
			}
		}
	}
	tel[x]=tmp;
	return ;
}

int main(){
	N=read(),M=read();
	for(int i=0;i<=N;i++)
	    for(int j=0;j<=M;j++) dp[i][j]=-INF;
	for(int i=1;i<=N-M;i++){
		int k=read();
		while(k--){
			int v=read(),w=read();
			addedge(i,v,w);
		}
	}
	for(int i=1;i<=M;i++) val[i+N-M]=read();
	dfs(1); int ans=M;
	while(dp[1][ans]<0) ans--;
	printf("%d\n",ans);
}

【樹形dp】TELE