POJ 1155

                                                 TELE

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6031   Accepted: 3360

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

大致题意:

广播站要向人们转播足球比赛,广播站在节点1,叶子节点为观众,其他点为中继器,信号从广播站经过中继器传送的时候会消耗一定的费用,而观众会支付一定的费用来观看,输出在不亏损的状况下最多能有多少人观看比赛。

解题思路:

dp当前有多少个人收看有多少收益,dp[now][j]=max(dp[now][j],dp[now][k]+dp[son][j-k]-tree[i].v),当前结点有j个人收看的时候的收益。

代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
int n,m,p;
int dp[3005][3005],h[3005],num[3005],ls[3005];
struct f
{
int now,v,son;
}tree[10005];
void build(int fa,int son,int cost)
{
tree[p].now=son;
tree[p].son=h[fa];
tree[p].v=cost;
h[fa]=p++;
}
void csh()
{
for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
        dp[i][j]=-9999999;
}
void dfs(int now)
{
for(int i=h[now];i!=-1;i=tree[i].son)
    {
    int to=tree[i].now;
    dfs(to);
    for(int j=0;j<=num[now];j++)
        ls[j]=dp[now][j];
    for(int j=0;j<=num[now];j++)
        for(int k=1;k<=num[to];k++)
            dp[now][j+k]=max(dp[now][j+k],ls[j]+dp[to][k]-tree[i].v);
    num[now]+=num[to];
    }
}
int main()
{
int k,a,c;
while(cin>>n>>m)
    {
    p=0;
    memset(h,-1,sizeof(h));
    for(int i=1;i<=n-m;i++)
        {
        scanf("%d",&k);
        num[i]=0;
        while(k--)
            {
            scanf("%d%d",&a,&c);
            build(i,a,c);
            }
        }
    csh();
    for(int i=n-m+1;i<=n;i++)
        {
        scanf("%d",&dp[i][1]);
        num[i]=1;
        }
    dfs(1);
    for(int i=m;i>=0;i--)
        if(dp[1][i]>=0)
            {
            cout<<i<<endl;
            break;
            }
    }
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40421671/article/details/88688629
今日推荐