# (Tree backpack / multi-branch tree knapsack problem) Luo Gu P1273 cable networks (increase + / provincial election -)

Title Description

A premium cable television network plans to broadcast an important football match. They broadcast networks and user terminals constitute a tree structure, the root of this tree is located in the football game scene, leaves for each user terminal, a transit point for other internal nodes of the tree.

From the relay station to the relay station, and signal transmission from the relay station to the cost of all the user terminals are known, the total cost of a broadcast transmission signal is equal to the sum of the costs.

Each user now prepare a fee want to watch this exciting football game, the cable television network to which users have the right to decide not to provide signals which provide signals to the user.

Write a program to find a solution allows the user to watch cable television broadcast as much as possible without losing money.

Input Format

The first line of the input file contains two space-separated integers N and M, where 2≤N≤3000,1≤M≤N-1, N is the total number of nodes throughout a cable television network, M being a user terminal number.

The first relay station that is the root of the tree is numbered 1, other broadcast station numbered 2 to NM, the user terminal number NM + 1 to N.

NM subsequent lines each represents - a data broadcast station, i + 1-line data indicating the i-th relay stations, in the following format:

K A1 C1 A2 C2 ... story ready

K represents the K access nodes (relay station or subscriber) at the relay station, each node corresponds to a pair of integers A and C, A represents a node number, C represents the cost of the current relay station transmits a signal to the node A . The last line in turn means that all users to watch the game and be prepared to pay an amount of money.

Output Format

A line output file only contains an integer representing the maximum number of users required above problems.

Sample input and output

Input # 1
5 3
2 2 2 5 3
2 3 2 4 3
3 4 2
Output # 1
2

Description / Tips

Sample interpretation

As shown, there are five nodes. ① is the root node, i.e. live station, a transfer station ②, ③④⑤ to the UE, a total of M, the amount of money from the number N-M + 1 to N, they are prepared to watch the game is 3, 4,2, a signal may be transmitted from node ① to node ②, the cost is 2, can also send a signal to the node ⑤, costs (data shown in the second row) 3, can be transmitted from node ② to node signals point ③, costs 2. May also transmit signals to the node ④, a cost of 3 (data shown in the third row), if all of the user (③④⑤) can fancy game, the total cost of signal transmission:

2 + 3 + 2 + 3 = 10, greater than the user is willing to pay the total cost of 3 + 4 + 2 = 9, the cable network will lose money, but only allow two users to watch the game ③④ not lost money.

 Thinking:
Up from the analysis, for all leaf nodes of the parent node, the parent node reaches the maximum value from the root node: The parent node to route the right leaf node value value xi, leaf nodes yi, is maxΣ (yi- xi);
Select the arbitrary leaf node, the parent node to maximize the value (as much as possible connectivity son), while ensuring no loss, need to store the maximum amount of money that the remaining node
This state came out,
F [i] [j] indicates to a parent node i, j is the number of remaining money for the maximum number of son --- 1
Or F [i] [j] i represented by a parent node, there is a remaining amount of money j --- 2 sons
Try pushing 1:
For state i, which is set son ai, f [i] [j] = max (f [i] [j], f [ai] [j + path [i] [a [i]]]);
For all leaf nodes i, f [i] [money [i]] = 1;
Pushing 2:
Initialization: For all leaf nodes i, f [i] [1] = money [i];
f [i] [j] = max (f [i] [j], f [i] [jk] + f [or] [k]);

Correct answer:

DP tree (packet backpack)

Talk about their own understanding

1. Identify DP [i] [j] meaning, represents a node i, j selected from the user, to get the maximum value of money, and then do the backpack packet to each node.

2. The grouping is to see how the backpack? First, the total capacity of the pack corresponding to the number of points for all users subtree of the root node (dp [i] [j] j can not exceed the number of all users connected to it). Then, each of the son node as a group, each one selected from the elements, selected from the group selected from n ... two users.

3. The transfer equation dp [i] [j] = max (dp [i] [j], dp [] [jk] + dp [v] [k] - cost of this edge) i, j not explained , v enumerations to this group (i.e., the son i), k represents an enumeration of the elements in this group: k-th user is selected.

4. The final output dp [1] [i]> = maximum value of i is 0, then the reverse enumeration.

AC Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#define For(i,a,b) for(int i=a;i<=b;i++)//简化代码
#define maxn 3001
using namespace std;
int n,m;
int dp[maxn][maxn],val[maxn];
inline int max(int x,int y)//手写max、min函数
{
return x>y?x:y;
}
inline int min(int x,int y)
{
return x<y?x:y;
}
inline int read()//读入优化
{
char ch;
while((ch=getchar())<'0' || ch>'9');
int res=ch-48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return res;
}
struct node{//邻接表,不赘述
int next;
int to;
int w;
};
Node E [MAXN * 2 + 10];
int head [MAXN * 2 + 10], NUM;
void addedge (int from, int to, int weight)
{
E [NUM ++] = .next [from] head ;
E [NUM] .to = to;
E [NUM] .W = weight;
head [from] NUM =;
}
int DFS (int u) // dp most important function of the processing node u, u return node the maximum number of clients
{
IF (u> nm) // If the node is a leaf node u, i.e. client port
{
DP [u] [. 1] Val = [u]; // you can only provide a customer, the presence of value Val [u]
return. 1; // returns the number of the customer. 1
}
int SUM = 0, T; the maximum number of client nodes u @ SUM statistics (i.e., a packet knapsack capacity), t is stored
// current son node number of the customer, the classification of all sons, through all its sons grouping backpack
for (int i = head [u ]; i; i = e [i] .next) // all sons, i.e. packet backpack All categories
{
int V = E [I] .to; //
T = DFS (V); // determine the capacity of the group son
sum + = t; // total capacity of statistical
for (int j = sum; j > 0; j -) // packet backpack, the backpack outer volume enumeration
{
for (int. 1 = K; K <= T; K ++) // inner group enumeration son case
// In fact, can also be optimized binary eh!
if (jk> = 0) // if the son can be transferred from the over
dp [u] [j] = max (dp [u] [j], dp [u] [jk] + dp [v] [k] -e [I] .W);
} // update value then stored node u jk customers plus k v customer node provides, minus
// tolls can!
}
Return SUM; // return
}

main int ()
{
Memset (DP, ~ 0x3F, the sizeof (DP)); // array assigned to a peak value DP
n-= Read (), m = Read ();
the For (I,. 1, nm)
{
int size read = ();
the For (J,. 1, size) // read side
{
int V = read (), W = read ();
addedge (I, V, W);

}
}
the For (I, m + n-- 1, n) // read client port
Val [I] = read ();
the for (I,. 1, n-)
DP [I] [0] = 0;
DFS (. 1);
for (int I = m; I> . 1 =; i--)
IF (DP [. 1] [I]> = 0) // first loss not meet up to customers!
{
The printf ( "% D", I); // can output!
BREAK;
}

return 0;
}

Multi-branch tree knapsack problem:

https://blog.csdn.net/no1_terminator/article/details/77824790

https://blog.csdn.net/qq_30802053/article/details/78154266

https://blog.csdn.net/u013582254/article/details/78409114

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11432704.html