poj 1155(树型dp)

来源:互联网 发布:xshell mac 代替 编辑:程序博客网 时间:2024/06/04 03:38
TELE
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5037 Accepted: 2758

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 63 2 2 3 2 9 32 4 2 5 23 6 2 7 2 8 24 3 3 3 1 1

Sample Output

5


题意:
有一个发送站,n-m个中转站,m个用户,每次传输会有花费,每个用户会有收益,求总收益不小于0时的最大用户数。


思路:
用dp[i][j]代表当前站位i时有j个用户的花费,花费可以为负。dp的递推公式为dp[i][j+k]=max(dp[i][j+k],dp[i][j]+dp[v][k]-cost[i][v])。其中v是i的可到达点。


#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#include<bitset>#include<queue>#include<stack>#include<list>#include<set>#include<math.h>#include<map>using namespace std;const int N=3005;int n,m,val[N],num[N],tem[N],dp[N][N];int INF=1e9;struct edge{    int to,w,next;    edge(int x,int y,int z):to(x),w(y),next(z){}    edge(){}};edge e[3*N];int tot,head[N];void addedge(int from,int to,int w){    e[tot]=edge(to,w,head[from]);    head[from]=tot++;}void dfs(int node){    for(int i=head[node];i!=-1;i=e[i].next)    {        int v=e[i].to;        dfs(v);        for(int j=0;j<=num[node];j++)            tem[j]=dp[node][j];        for(int j=0;j<=num[node];j++)            for(int k=1;k<=num[v];k++)                dp[node][j+k]=max(dp[node][j+k],tem[j]+dp[v][k]-e[i].w);        num[node]+=num[v];    }}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);    scanf("%d%d",&n,&m);    tot=0;    memset(head,-1,sizeof(head));    memset(num,0,sizeof(num));    dp[1][0]=0;    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++)    {        int k;        scanf("%d",&k);        for(int j=0;j<k;j++)        {            int v,w;            scanf("%d%d",&v,&w);            addedge(i,v,w);        }    }    for(int j=n-m+1;j<=n;j++)    {        scanf("%d",&dp[j][1]);        num[j]++;    }    dfs(1);    for(int i=m;i>=0;i--)        if(dp[1][i]>=0)        {            printf("%d\n",i);            break;        }return 0;}


0 0
原创粉丝点击