[1273] Luo Valley cable networks

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.

 

Solution: Oh DP is still a tree

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define M 3005
using namespace std;
int n,m,nxt[M],head[M*6],to[M*6],adj[M],son[M],money[M],f[M][M],tot;
int read(){
    char c=getchar();int ans=0;
    while (c<'0'||c>'9') c=getchar();
    while (c>='0'&&c<='9') ans=(ans<<1)+(ans<<3)+(c^48),c=getchar();
    return ans;
}
void add(int u,int v,int w){
    nxt[++tot]=head[u];head[u]=tot;to[tot]=v;adj[tot]=w;
    return;
}
void dfs(int x){
    if (!head[x]){f[x][0]=0;son[x]=1;f[x][1]=money[x-n+m];return;}
    for (int i=1;i<=n;i++) f[x][i]=-1000000000;
    for (int i=head[x];i;i=nxt[i]){
        dfs(to[i]);son[x]+=son[to[i]];
        for (int k=son[x];k>=1;k--)
            for (int j=1;j<=min(k,son[to[i]]);j++)
               f[x][k]=max(f[x][k],f[x][k-j]+f[to[i]][j]-adj[i]);
    }
    return;
}
int main(){
    n=read(),m=read();
    for (int i=1;i<=n-m;i++){
        int k=read();
        for (int j=1;j<=k;j++){
            int A=read(),C=read();
            add(i,A,C);
        }
    }
    for (int i=1;i<=m;i++) money[i]=read();
    dfs(1);
    for (int i=m;i>=0;i--)
        if (f[1][i]>=0){printf("%d",i);return 0;}
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11242977.html