POJ1155TELE (树形DP)
来源:互联网 发布:c语言最大公约数的英文 编辑:程序博客网 时间:2024/04/29 23:43
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.
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.
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 63 2 2 3 2 9 32 4 2 5 23 6 2 7 2 8 24 3 3 3 1 1
Sample Output
5
解题:dp[p][m]:表示节点p上有m个user的最大剩于价值。
dp[p][m]=max(dp[p][m],dp[p][m-k]+dp[son][k]-cost);
最后只要最大剩于价值大于等于0,其中输出最多user个数即可。
#include<stdio.h>#include<iostream>#include<vector>using namespace std;#define INF -9999999typedef struct nnn{ int id,cost;}node;vector<node>map[3005];int dp[3005][3005],val[3005],k[3005],vist[3005],N,M;int max(int a,int b){ return a>b?a:b;}void dfs(int p){ vist[p]=1; k[p]=0; for(int i=0;i<=N;i++) dp[p][i]=INF; dp[p][0]=0; if(p>N-M) dp[p][1]=val[p],k[p]=1;//表示叶子节点 for(int i=0;i<map[p].size();i++) { int son=map[p][i].id; if(vist[son])continue ; dfs(son); k[p]+=k[son]; for(int m=k[p];m>=1;m--) for(int tk=1;tk<=k[son];tk++) dp[p][m]=max(dp[p][m],dp[p][m-tk]+dp[son][tk]-map[p][i].cost); }}int main(){ node p; int id,cost,tk; while(scanf("%d%d",&N,&M)>0) { for(int i=1;i<=N;i++) map[i].clear(),vist[i]=0; for(int i=1;i<=N;i++) if(i<=N-M) { scanf("%d",&tk); while(tk--) { scanf("%d%d",&id,&cost); p.id=id; p.cost=cost; map[i].push_back(p); p.id=i; p.cost=cost; map[id].push_back(p); } } else{ scanf("%d",&val[i]); } dfs(1); for(int i=N;i>=0;i--) if(dp[1][i]>=0) { printf("%d\n",i);break; } }}
0 0
- poj1155TELE - 树形dp
- POJ1155TELE (树形DP)
- 树形dp
- 树形DP
- 树形dp
- 树形DP
- 树形dp
- 树形DP
- 树形DP
- 树形DP
- 树形DP
- 树形dp
- 树形dp
- 树形dp
- 树形dp
- 树形DP
- 树形DP
- 树形DP
- long double型变量的输出问题
- 膜拜大神
- 社会名流问题的解答----算法引论学习
- 河南科技学院-第五届网页设计大赛-见面会暨第一次讲课
- 使用chrome 插件 printwhatyoulike 打印乱码
- POJ1155TELE (树形DP)
- javascript学习笔记
- 详细介绍SWFUpload
- talend的常用控件使用教程
- 百度高级语法使用及SA权限
- 设计模式——抽象工厂模式
- 评论问题
- 好久没来了,朋友们还好么。
- VIM窗口分屏