您的位置:首页 > 其它

POJ1155---TELE(树形dp,背包)

2015-05-10 21:08 316 查看
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

dp[u][i] 表示 以u为根的子树,取i个叶子节点,可以得到的最大价值

dp[u][i]=max(dp[u][i],dp[u][j]+dp[v][i−j]−cost[u][v])dp[u][i] = max(dp[u][i], dp[u][j] + dp[v][i - j] - cost[u][v])

/*************************************************************************
    > File Name: POJ1155.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月10日 星期日 20时13分27秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 3010;
int dp

;
int val
;
vector <PLL> edge
;
int sum
;
int n, m;

void dfs(int u) {
    int size = edge[u].size();
    if (size == 0) {
        sum[u] = 1;
        return;
    }
    for (int i = 0; i < size; ++i) {
        int v = edge[u][i].first;
        dfs(v);
        sum[u] += sum[v];
    }
}

void DP(int u) {
    int size = edge[u].size();
    if (size == 0) {
        dp[u][1] = val[u - n + m];
        sum[u] = 1;
        return;
    }
    for (int i = 0; i < size; ++i) {
        int v = edge[u][i].first;
        DP(v);
        sum[u] += sum[v];
    }
    dp[u][0] = 0;
    for (int i = 0; i < size; ++i) {
        int w = edge[u][i].second;
        int v = edge[u][i].first;
        for (int j = sum[u]; j >= 1; --j) {
            for (int k = 0; k <= sum[v] && k <= j; ++k) {
                if (j < k) {
                    continue;
                }
                dp[u][j] = max(dp[u][j], dp[u][j - k] + dp[v][k] - w);
            }
        }
    }
}

int main() {
    while (~scanf("%d%d", &n, &m)) {
        for (int i = 1; i <= n; ++i) {
            edge[i].clear();
        }
        for (int i = 1; i <= n - m; ++i) {
            int size;
            int v, w;
            scanf("%d", &size);
            for (int j = 1; j <= size; ++j) {
                scanf("%d%d", &v, &w);
                edge[i].push_back(make_pair(v, w));
            }
        }
        for (int i = 1; i <= m; ++i) {
            scanf("%d", &val[i]);
        }
        memset(sum, 0, sizeof(sum));
        memset(dp, -inf, sizeof(dp));
        DP(1);
        int ans = 0;
        for (int i = m; i >= 0; --i) {
            if (dp[1][i] >= 0) {
                ans = i;
                break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: