Topic description
A pay cable TV network plans to relay an important football match. Their broadcast network and user terminal constitute a tree structure, the root node of the tree is located at the scene of the football match, the leaves for each user terminal, and other transit points for the tree's internal nodes.
Signal transmission costs from the retransmission station to the retransmission station and from the retransmission station to all user terminals are known, and the total cost of a retransmission is equal to the sum of the cost of the transmitted signal.
Now each user is prepared for a fee to watch this wonderful football game, the cable network has the right to decide which users will be given a signal without giving them a signal.
Write a program to find a solution that allows the cable network to make as many users as possible to watch the broadcast without losing money.
Input output Format input format:
The first line of the input file contains two integers separated by spaces \ (n\) and \ (m\) , where \ (2 \leq N \leq 3000\) , \ (1 \leq M \leq n-1\) , \ (n\) is the total number of nodes for the entire cable network, \ (m\) is the number of user terminals.
The first retransmission station is the root node of the tree is numbered \ (1\) , the other broadcast station number is \ (2\) to \ (n-m\) , the user terminal number is \ (n-m+1\) to \ (n\) .
The next \ (n-m\) line represents-the data for the retransmission station, and the i+1\ line represents the data for the i\ retransmission station in the following format:
\[K \ a_1 \ c_1 \ a_2 \ c_2 ... A_k \ C_k \]
\ (k\) indicates that the retransmission station ( k\) node (broadcast station or user), each node corresponding to a pair of integers \ (a\) and \ (c\) , \ (a\) Represents the node number, \ (c\) represents the cost of transmitting a signal from the current retransmission station to the node \ (a\) . The last line in turn represents the amount of money that all users are prepared to pay for watching the game.
Output format:
The output file has only one row and contains an integer that represents the maximum number of users required for the above problem.
Input and Output Sample input example:
5 32 2 2 5 32 3 2 4 33 4 2
Sample output:
2
Description
Sample explanation
, a total of five nodes. Node \ (①\) is the root node, the live broadcast station, \ (②\) as a broker, \ (③④⑤\) is the client, \ (m\) , numbered from \ (n-m+1\) to \ (n\) , the amount of money they prepared for watching the game was \ (3, 4, 2\) , from node The span class= "math inline" >\ (①\) can transmit signals to the node \ (②\) at a cost of \ (2\) , can also transmit signal to node \ (⑤\) at a cost of \ (3\) (shown in the second row of data), from node \ (②\) can transmit signal to node \ (③\) , costs \ (2\) . It is also possible to transmit the signal to the node \ (④\) at a cost of \ (3\) (as shown in the third row of data), if you want to have all users ( span class= "Math inline" >\ (③④⑤\) ) can look at the game, the total cost of signal transmission is:
\ (2+3+2+3=10\) , more than the user is willing to pay the total cost of (3+4+2=9\) , the cable network is losing money, and only let \ (③④\) Two users watching the game will not lose money.
Ideas
Liver Test field! -uranus
Set \ (dp[i][j]\) indicates that the \ ( i\) node satisfies \ (j\) The maximum benefit of the audience, then we can update it with each son node of the \ (i\) node.
\[Dp[i][j]=max \{Dp[i][j-k]+dp[w][k]-cost[i][w] (w \in son[i]) \} \]
Then just do this dynamic planning in the process of \ (dfs\) .
AC Code
#include <bits/stdc++.h>using namespace std;const int Maxn=3005;const int Inf=1000000;int n,m,ans,cnt[maxn],sz[ Maxn],son[maxn][maxn],c[maxn][maxn],dp[maxn][maxn];int read () {int re=0; Char Ch=getchar (); while (!isdigit (CH)) Ch=getchar (); while (IsDigit (CH)) re= (re<<3) + (re<<1) +ch-' 0 ', Ch=getchar (); return re;} void Dfs (int now) {for (int i=1;i<=cnt[now];i++) {DFS (son[now][i]); Sz[now]+=sz[son[now][i]]; for (int k=sz[now];k>=0;k--) for (int j=0;j<=sz[son[now][i]];j++) Dp[now][k]=max (Dp[now][k], Dp[now][k-j]+dp[son[now][i]][j]-c[now][i]); }}int Main () {N=read (), M=read (); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) Dp[i][j]=-inf; for (int i=1;i<=n-m;i++) {cnt[i]=read (); for (int j=1;j<=cnt[i];j++) Son[i][j]=read (), C[i][j]=read (); } for (int i=n-m+1;i<=n;i++) sz[i]=1,dp[i][1]=read (); DFS (1); for (int i=m;i>=0;i--) if (Dp[1][i]>=0) {printf ("%d", I); return 0; }}
Luogu P1273 Cable TV network (tree-shaped dp+ backpack)