poj 1155 TELE

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

Status

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


DP题,又一次深刻地感受到自己DP的弱。。。

题意:(摘自松AK)

给出一棵树,一共n个节点(根节点为电视台),其中有m个叶子结点(用户)依次为n-m+1~n。
接下来n行描述非叶子节点1~n,首先是一个k,表示它有k个子节点。接下来,每两个数描述一个节点的编号及与这个节点的距离(经过此路线的费用)。
最后一行n-m+1包括这些点(用户)拥有的钱数。
本题要求的是电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号。

思路嘛,好多都讲得不是很清楚。

我们定义dp[i][j],  为以i为根节点,给j个用户发送信号所得到的最大剩余费用。

我们遍历一棵树时,按dfs顺序,一棵一棵子树地遍历, 算dp[i][j]时按一棵一棵子树去更新。比如到了u,遍历到它的子节点v, 那么dfs(v), 然后dp[u][j+k] = max(dp[u][j+k], dp[u][j]+dp[v][k]-d);  (d: u,v之间的路程) j与k分别是(0-size[u])与(0-size[v]), size[i]表示dfs到当前阶段时i的子节点个数,所以弄完v以后还有一句size[u] += size[v];

    初值叶子节点赋值就是了,还有一点注意是上面的dp[u][j]需要在前面弄一个t[]把dp[u][]存下来,再用t[]代替dp[u][],不然在算的时候会有覆盖现象。

//poj 1155 
//miaomiao_ymxl 2016.9.3
//
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

#define MAXN (3000+5)
#define INF 0x7f7f7f7f

struct node{
    int v, dis;
};

vector<node> G[MAXN];
int size[MAXN], dp[MAXN][MAXN], t[MAXN];

void dfs(int u){
    for(int i = 0; i < G[u].size(); i++){
        int v = G[u][i].v, d = G[u][i].dis;
        dfs(v);
        
        for(int j = 0; j <= size[u]; j++) t[j] = dp[u][j];
        for(int j = 0; j <= size[u]; j++)
          for(int k = 0; k <= size[v]; k++)
            dp[u][j+k] = max(dp[u][j+k], dp[v][k]+t[j]-d);
        
        size[u] += size[v];
    }
}

int main(){
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
    
    int n, m;
    scanf("%d%d", &n, &m);
    
    for(int i = 1; i <= n; i++)
      for(int j = 1; j <= n; j++) dp[i][j] = -INF;
    
    for(int i = 1; i <= (n-m); i++){
        int ni;
        scanf("%d", &ni);
        
        for(int j = 1; j <= ni; j++){
            int v, dis;
            scanf("%d%d", &v, &dis);
            
            G[i].push_back((node){v, dis});
        }
    }
    
    for(int i = (n-m+1); i <= n; i++){
        int x;
        scanf("%d", &x);
        
        dp[i][1] = x;
        size[i] = 1;
    }
    
    dfs(1);
    for(int i = n; i; i--)
        if(dp[1][i] >= 0){
            printf("%d\n", i);
            break;
        } 
    
    return 0;
}
Because of you, I still fight on it, I belive I can do it!


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值