博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1155 TELE
阅读量:6119 次
发布时间:2019-06-21

本文共 3378 字,大约阅读时间需要 11 分钟。

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4976   Accepted: 2734

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 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

Source

 

树形DP,f[当前结点][连接的客户端数]=花费

最后对f[根节点][j]扫描,找到使f为正的最大j,即是答案。

1 /*by SilverN*/ 2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 const int INF=0x3f3f3f3f; 9 const int mxn=3200;10 struct edge{11 int v,dis;12 int nxt;13 }e[mxn];14 int hd[mxn],cnt=0;15 void add_edge(int u,int v,int dis){16 e[++cnt].v=v;e[cnt].dis=dis;e[cnt].nxt=hd[u];hd[u]=cnt;17 return;18 }19 int n,m;20 int num[mxn];21 int f[mxn][mxn];22 void dp(int u){23 int i,j,k;24 for(i=hd[u];i;i=e[i].nxt){25 int v=e[i].v;26 dp(v);27 num[u]+=num[v];28 for(j=num[u];j;--j){29 for(k=num[v];k;--k){30 f[u][j]=max(f[u][j],f[u][j-k]+f[v][k]-e[i].dis);31 }32 }33 }34 return;35 }36 int main(){37 scanf("%d%d",&n,&m);38 int i,j,k;39 int u,v,d;40 for(i=1;i<=n-m;i++){41 scanf("%d",&k);42 for(j=1;j<=k;j++){ 43 scanf("%d%d",&v,&d);44 add_edge(i,v,d);45 }46 }47 for(i=1;i<=n;i++)48 for(j=1;j<=m;j++)49 f[i][j]=-INF;50 for(i=n-m+1;i<=n;i++){num[i]=1;scanf("%d",&f[i][1]);}51 dp(1);52 for(i=m;i>=0;i--){53 if(f[1][i]>=0){54 printf("%d\n",i);55 break;56 }57 }58 return 0;59 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5916227.html

你可能感兴趣的文章
简单理解js的prototype属性
查看>>
Halcon算子翻译——dev_set_check
查看>>
Python 字符串
查看>>
Django 0.4
查看>>
提取奖励办数据中人员信息(自用)
查看>>
vc 中调用COM组件的方法
查看>>
字符串查找
查看>>
如何让命令行支持PHP语法编译
查看>>
html代码
查看>>
神经网络公式推导
查看>>
java基础之集合&数组
查看>>
lib 和 dll 的区别、生成以及使用详解
查看>>
部署openssh服务器
查看>>
基于 Kong 和 Kubernetes 的 WebApi 多版本解决方案
查看>>
get-random生成电话号码
查看>>
Fabric密码保存
查看>>
【Android每日一讲】2012.11.02 全屏幕以按钮重写 - 动态产生按钮并最大化
查看>>
10折交叉验证(10-fold Cross Validation)与留一法(Leave-One-Out)、分层采样(Stratification)...
查看>>
IIS 7.5: HOW TO ENABLE TLS 1.1 AND TLS 1.2
查看>>
Node.js中针对中文的查找和替换无效的解决方法
查看>>