poj1155 TELE(树形dp+背包)

来源:互联网 发布:mac的excel数据有效性 编辑:程序博客网 时间:2024/05/17 06:42
TELE
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4344 Accepted: 2352

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

Source

Croatia OI 2002 Final Exam - Second Day

题意:给定一棵树,1为根结点表示电视台,有m个叶子节点表示客户,有n-m-1个中间节点表示中转站,每条树边有权值。现在要在电视台播放一场比赛,每个客户愿意花费cost[i]的钱观看,而从电视台到每个客户也都有个费用,并且经过一条边只会产生一个费用。问电视台不亏损的情况最多有几个客户可以看到比赛
分析:本题在思考的时候可分两种情况:1、当节点为叶子节点时,每个用户都要支付Money,那当前选一个的价值为Money值,中转站和电视台要计算的是在保证不亏损也就是从用户那获得得价值减去中转费用必须大等于0,那么叶子节点的Money值就是基本信息。2、当节点为中转站或者非叶子节点时,每次从子节点中选择n个并计算收取的Money和中转费用,如果两者想减大等于0就用n来更新答案。怎么转换到分组背包呢?可以这样想,每次从每个子节点中都只能取若干个,不可能重叠着取,那几个节点就是几组背包,最大容量是这点的叶子子孙数量,选几个节点就是选择的容量,价值就是用户给的Money-中转费用。

#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-6;const double pi = acos(-1.0);const int INF = 0x3f3f3f3f;const int MOD = 1000000007;#define ll long long#define CL(a,b) memset(a,b,sizeof(a))#define MAXN 3010int n,m,pre;int money[MAXN],dp[MAXN][MAXN];//money表示每条权的权值int ans,sum[MAXN],vis[MAXN];//sum表示每个结点的最优解,vis标记struct node{    int v,len;//len为权值    node *next;}*head[MAXN*2], tree[MAXN*2];void add(int x, int y, int len){    tree[pre].v = y, tree[pre].len = len;    tree[pre].next = head[x], head[x] = &tree[pre++];    tree[pre].v = x, tree[pre].len = len;    tree[pre].next = head[y], head[y] = &tree[pre++];}void dfs(int v, int len){    if (vis[v]) return ;    vis[v] = 1, dp[v][0] = 0;    int tot = 0;    node *p = head[v];    while(p != NULL)    {        if(!vis[p->v])        {            tot++;            dfs(p->v, len);            sum[v] += sum[p->v];        }        p = p->next;    }    if(tot == 0)//根结点    {        sum[v] = 1;        dp[v][1] = money[v]-len;    }    else    {        p = head[v];        while(p != NULL)        {            int k=p->v, len=p->len;            for(int j=sum[v]; j>=1; j--)//背包求解            {                for(int i=1; i<=sum[k]; i++)                {                    if (j >= i&&dp[v][j-i]!=-INF&&dp[k][i]!=-INF)                        dp[v][j] = max(dp[v][j], dp[v][j-i]+dp[k][i]-len);                }            }            p = p->next;        }    }}int main(){    int a,b,k;    while(cin>>n>>m)    {        pre=1; ans=-INF;        CL(vis, 0);        CL(sum, 0);        CL(head, NULL);        for(int i=0; i<=n; i++)            for(int j=0; j<=n; j++)            dp[i][j]=-INF;        for(int i=1; i<=n-m; i++)        {            cin>>k;            for(int j=1; j<=k; j++)            {                cin>>a>>b;                add(i, a, b);            }        }        for (int i=n-m+1; i<=n; i++)            cin>>money[i];        dfs(1, 0);        for(int i=n; i>=0; i--)            if(dp[1][i] >= 0){cout<<i<<endl; break;}    }    return 0;}


0 0