F - TELE

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


      题意:信号塔(根1)和用户(叶子)以及中继器(其他点)组成树,每条边有代价,求电视台不赔钱的情况下最多能有多少人看到电视。

      用 dp[MAX][MAX]; //以i为根的子树,j个观众的利润,dp公式:dp[now][j]=max(dp[now][j],  dp[now][j-k]+dp[next][k]-edge[i].len);//当前节点为根j个观众,与分给子节点k个 + 自己留下j-k个 - 当前边,取最大值。这个题里面多设了一个num【】数组,//以i为根的子树,有多少个观众(叶子),只是用在了背包第二重里面,本来感觉多余,去了之后超时,才明白这里的作用,简化了背包循环次数,至于是简化到了O(什么)我也不会算,但是这里确实很巧妙。

#include<iostream>
#include<cstring>
using namespace std;
const int MAX=3010;
struct Edge
{
    int next;
    int to;
    int len;
}edge[MAX*2];
int head[MAX];
int num[MAX];//以i为根的子树,有多少个观众(叶子)
int dp[MAX][MAX];//以i为根的子树,j个观众的 利润
int money[MAX];
int vis[MAX];
int K;
int N, M;
void addEdge(int u, int v, int len)
{
    edge[++K].next=head[u];
    edge[K].to=v;
    edge[K].len=len;
    head[u]=K;
}
void dfs(int now)
{
    vis[now]=1;
    if(now>=N-M+1)//叶子节点
    {
        dp[now][1]=money[now];
        num[now]=1;
        //cout<<"now=  "<<now<<"  "<<num[now]<<endl;
        return ;
    }
    for(int i=head[now]; i!=0; i=edge[i].next)//i 边号
    {
        int next=edge[i].to;//next 下个点
        if(vis[next])
            continue;
        dfs(next);
        for(int j=M; j>=0; j--)//背包
        {
            for(int k=num[next]; k>=0; k--)
            //for(int k=0; k<=j; k++)//背包这一步超时,num【】的作用
            {
                if(j-k>=0)
                    dp[now][j]=max(dp[now][j], dp[now][j-k]+dp[next][k]-edge[i].len);
            }
        }
        num[now]+=num[next];
    }
    //cout<<"now=  "<<now<<"  "<<num[now]<<endl;
    return ;
}
int main()
{
    ios::sync_with_stdio(0);
    int k, v, len;
    while(cin>>N>>M)
    {
        K=0;
        memset(dp, -0x3f, sizeof(dp));
        memset(num, 0, sizeof(num));
        memset(vis, 0, sizeof(vis));
        memset(edge, 0, sizeof(edge));
        memset(head, 0, sizeof(head));
        for(int i=0; i<=N; i++)
            dp[i][0]=0;
        for(int i=1; i<=N-M; i++)
        {
            cin>>k;
            for(int j=1; j<=k; j++)
            {
                cin>>v>>len;
                addEdge(i, v, len);
                addEdge(v, i, len);
            }
        }
        for(int i=N-M+1; i<=N; i++)
        {
            cin>>money[i];
        }
        dfs(1);
        int ansdex=0;
        for(int i=1; i<=M; i++)
        {
            //cout<<i<<"  dp  "<<dp[1][i]<<endl;
            if(dp[1][i]>=0)//不赔钱就行
            {
                ansdex=i;
            }
        }
        cout<<ansdex<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值