POJ 1155(TreeDP)
来源:互联网 发布:js数组slice 编辑:程序博客网 时间:2024/06/09 14:21
TELE
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2363 Accepted: 1167
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.
这个是一道很水的TreeDP。解释完毕。
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<vector>#define maxn 3100#define Clear(a,b) memset(a,b,sizeof(a))using namespace std;struct edge{ int ed,cost; };vector <edge> links[maxn];int user[maxn],val[maxn],dp[maxn][maxn];int Max(int a,int b){ if (a > b) return a; return b;}void dfs(int k){ int m = links[k].size(); int i,cost,son,j,c; if (m == 0) { user[k] = 1; dp[k][1] = val[k]; //printf("%d:",k); //for(i = 1;i <= user[k];i++) printf(" %d",dp[k][i]); //printf("\n"); return ; } else user[k] = 0; for(i = 0;i < m;i++) { dfs(links[k][i].ed); user[k] += user[links[k][i].ed]; } for(i = 0;i < m;i++) { son = links[k][i].ed; cost = links[k][i].cost; for(c = user[k];c >= 1;c--) for(j = 1;j <= user[son];j++) if (c >= j) { dp[k][c] = Max(dp[k][c],dp[k][c-j] + dp[son][j] - cost); //printf("%d %d %d %d\n",k,son,dp[son][j],cost); } } //printf("%d:",k); //for(i = 1;i <= user[k];i++) printf(" %d",dp[k][i]); //printf("\n"); return ;}int work(){ int n,m,t,i,j,a,b,ans; edge tmp; if (scanf("%d%d",&n,&m) == EOF) return 0; for(i = 0;i <= n;i++) for(j = 0;j <= n;j++) dp[i][j] = -100000000; for(i = 1;i <= n;i++) dp[i][0] = 0; Clear(user,0); Clear(val,0); int mm = n - m; for(i = 1;i <= n;i++) links[i].clear(); for(i = 1;i <= mm;i++) { scanf("%d",&t); for(j = 1;j <= t;j++) { scanf("%d%d",&a,&b); tmp.ed = a; tmp.cost = b; links[i].push_back(tmp); } } //printf("OK\n"); for(i = 1;i <= m;i++) scanf("%d",&val[i+mm]); dfs(1); //printf("OK\n"); ans = m; while(dp[1][ans] < 0) ans--; printf("%d\n",ans); return 1;} int main(){ while(work()); return 0;}
- POJ 1155(TreeDP)
- POJ-1848-treedp
- POJ-2152-treeDP
- POJ-1741-treeDP+分治
- POJ 3162(TreeDP+单调队列)
- POJ-3162-treeDP+线段树
- POJ 3162(TreeDP+单调队列)Walking Race
- POJ 3162 Walking Race TreeDP+双单调队列 ★
- pku2486 treedp
- BZOJ2435【TreeDP】
- HDU P1520(TreeDP)
- HDU P2196(TreeDP)
- HDU P1011(TreeDP)
- HDU-4354-treeDP
- HDU-4008-treeDP
- IOI2005 river treedp
- hdu 1561 treedp
- HDU1520 TreeDp 入门
- 为啥QQ登录一直要求输入验证码
- Qt下的OpenGL 编程(3)绘制平面几何体
- oracle的sqlnet.ora,tnsnames.ora,listener.ora三个配置文件
- poj 1953
- 关于eclipse的link方式安装插件问题
- POJ 1155(TreeDP)
- 有关View的几个基础知识点-IOS开发
- Oracle中文乱码解决思路
- hdu 1042 N!
- XHProf安装使用笔记
- pure specifier can only be specified for functions
- [Puzzle toad 3] Take the last chip
- JAVA计算器【源码】
- magento 积分插件 J2T Reward Points + Referral program VA 下载