POJ1155 TELE
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1872 | Accepted: 865 |
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 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
*******************************************************************************
题目大意:给定一个广播电视的网络,1为根,共n个节点m个用户(即叶子节点),现在,从一个节点传到另一个节点所需的花费也给出。对于每个用户,都有一个值,表示该用户能提供的最大金钱数。问,在保证1这个点不亏本的情况下,最多的用户数。
解题思路:传说这道题目用的是泛化背包的思想,对于一个背包九讲只看了一半的人来说,我马上去看了那个泛化背包是什么。结果感觉怎么这么像分组背包,唉,不管了,就拿分组背包的思想来写呗,想想应该是同一回事情吧。泛化背包是给定一个值的函数,然后枚举这个值的变量,产生一堆物品,但是这一堆物品中,也只能选其中,那还是分组背包呗。于是就ac了,至于状态转移,不是太难。对于每个节点的子树,枚举该节点的儿子,在每个儿子的情况下
dp[root][j]=max{dp[root][i],dp[root][i-s]+dp[k][s]-val[k]},s是儿子中的用户数,k是root的儿子,val是从root到k这个点的花费。边界条件:当root是叶子的时候,dp[root][1]=该用户可以提供的金钱;另外,dp[root][0]=0;
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#define N 3005
#define INF 0x3f3f
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;
struct E
{
short ed,val;
E(int a,int b):ed(a),val(b){}
};
vector<E>gra[N];
short n,m,fa[N],dp[N][N],pri[N],num[N];
void dpt(short s)
{
if(s>=n-m+1)
{
dp[s][1]=pri[s];
num[s]=1;
return ;
}
for(int i=0;i<gra[s].size();i++)
dpt(gra[s][i].ed);
dp[s][0]=0;
int vv=0;
for(int i=0;i<gra[s].size();i++)
{
short t=gra[s][i].ed,v=gra[s][i].val;
vv+=num[t];
for(int k=vv;k>=0;k--)
{
for(int j=1;j<=num[t];j++)
{
if(k<j)break;
dp[s][k]=MAX(dp[s][k],dp[t][j]+dp[s][k-j]-v);
}
}
}
num[s]=vv;
}
void re(void)
{
scanf("%d%d",&n,&m);
memset(fa,0,sizeof(fa));
memset(num,0,sizeof(num));
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
dp[i][j]=-1*INF;
for(int i=1;i<=n;i++)
gra[i].clear();
for(int i=1;i<=n-m;i++)
{
int k,a,c;
scanf("%d",&k);
for(int j=1;j<=k;j++)
{
scanf("%d%d",&a,&c);
gra[i].push_back(E(a,c));
}
}
for(int i=n-m+1;i<=n;i++)
scanf("%d",&pri[i]);
}
void run(void)
{
dpt((short)1);
int ans;
for(ans=n;ans>=0&&dp[1][ans]<0;ans--);
printf("%d\n",ans);
}
int main()
{
re();
run();
}