Algorithm: POJ 1155 TELE (tree-type backpack DP)

Source: Internet
Author: User
Tags int size pack

Meaning

A premium cable television network plans to relay an important football match. Their broadcast network and user terminals constitute a tree-like structure, the root of the tree is located in the scene of the football game, leaves for each user terminal, the other transfer station for the tree's internal nodes.

The cost of signaling from the relay station to the relay station and from the repeater station to all user terminals is known, and the total cost of a broadcast is equal to the sum of the cost of transmitting the signal.

Now every user has a fee to watch this wonderful football game, the cable network has the right to decide which users to provide signals to those who do not provide signals.

Write a program to find a solution that allows the cable network to make as many viewers as possible without losing money.

Ideas

Getting Started with a tree-shaped backpack.

F (i, j), representing the maximum return value of the subtree I relay to J users

This problem can be seen as a packet pack on a tree, each of which is considered a group of items, which can take 1, 2 ... j

And then there's the algorithm for the packet pack.

f (i, 1) = W (i) When point I is a leaf node

F (i, j) = max{f (i, j-k) + f[v][k]-W (i, V) | V is the son node of I, 0<=k<=j}

Code

/**===================================================== * is a solution for ACM/ICPC problem * * @source: poj-1 TELE * @type: Tree-Type Backpack DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com * Copyrig
HT (C) 2013/08/18 19:29 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> # Include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring
    
> #define MP make_pair using namespace std;
typedef pair<int, int >PII;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
    
Const double PI = ACOs (-1.0);
    
const int MAXN = 3010; namespace adj{int size, HEAD[MAXN]; struct Node {int V, next, W;}
E[MAXN];
void Initadj () {size = 0; memset (head,-1, sizeof (head));} void Addedge (int u, int v, int w) {e[size].v = v;
E[SIZE].W = W;
E[size].next = Head[u];
Head[u] = size++;
The using namespace Adj; //////////////int n, m;
int VAL[MAXN];
    
int F[MAXN][MAXN];
    
Returns a few leaf nodes int dfs (int u) {//init for (int i = 1; I <= m; ++i) f[u][i] =-inf;
    
if (head[u] = =-1) {f[u][1] = Val[u]; return 1;}
int sum = 0;
    
for (int e = head[u]; e!=-1; e = e[e].next) {int v = e[e].v, w = E[E].W;
int numleaf = DFS (v);
sum + + numleaf; Make a packet backpack//http://www.bianceng.cn for (int s = sum; s >= 1;--s) {for (int k = 0; k <= numleaf && k <= S
++K) F[u][s] = max (F[u][s], f[u][s-k] + f[v][k] + W);
return sum; int main () {while (~scanf ("%d%d", &n, &m)) {Initadj ()-for (int i = 1; I <= n-m; ++i) {int x, V
, W;
scanf ("%d", &x);
    
while (x--) {scanf ("%d%d", &v, &w); Addedge (i, V, W);}
for (int i = n-m + 1; I <= n; ++i) {scanf ("%d", &val[i]); DFS (1);
for (int i = m; I >= 0;-i) {if (F[1][i] >= 0) {printf ("%d\n", I); break;}}}
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.