您的位置:首页 > 理论基础 > 计算机网络

poj1155之有限电视网络

2014-01-08 10:40 288 查看
TELE

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 3354Accepted: 1724
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 6
3 2 2 3 2 9 3
2 4 2 5 2
3 6 2 7 2 8 2
4 3 3 3 1 1

Sample Output
5

题意:有一个电视台要用网络转播节目,这种电视网络是一棵树,树的节点为中转站或者用户。树节点的编号为1-n,其中1为总站,2-(n-m)为中转站,n-m+1 - n为用户,电视节目从一个地方传到另一个地方都要费用,同时每一个用户愿意出相应的钱来付电视节目,现在要求在电视台不亏本的情况下,要泥鳅最多允许有多少个用户可以看到电视节目。
分析:对于以点u为根的树,求允许j个用户看电视,电视台亏本的最少钱,最后结果就是输出dp[1][i]<=0的i的最大值。

本题用滚动数组节省空间,具体相当于把数组dp[]里面的空间a~b部分分给节点u,c~d部分分给节点v...的思想

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <map>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=3000;
int n,m,size,val[MAX];
int head[MAX],dp[MAX],temp[MAX];

struct Edge{
	int v,w,next;
	Edge(){}
	Edge(int V,int W,int NEXT):v(V),w(W),next(NEXT){}
}edge[MAX];

void Init(int num){
	for(int i=1;i<=num;++i)head[i]=-1;
	size=0;
}

void InsertEdge(int u,int v,int w){
	edge[size]=Edge(v,w,head[u]);
	head[u]=size++;
}

int dfs(int u,int Id){
	int last=0,now=0,p=0;
	dp[Id]=0;
	for(int i=head[u];i != -1;i=edge[i].next){
		int v=edge[i].v,w=edge[i].w;
		now=dfs(v,Id+last+1);
		if(v>=n-m+1)++now,p=1;
		else p=0;
		for(int j=last+1;j<=last+now;++j)dp[Id+j]=INF;
		for(int j=last;j>=0;--j){
			for(int k=1;k<=now;++k){
				dp[Id+j+k]=min(dp[Id+j+k],dp[Id+j]+temp[k-p]+w-val[v]);
			}
		}
		last+=now; 
	}
	for(int i=1;i<=last;++i)temp[i]=dp[Id+i];
	return last;
}

int main(){
	int v,w,k;
	while(~scanf("%d%d",&n,&m)){
		Init(n);
		for(int i=1;i<=n-m;++i){
			val[i]=0;
			scanf("%d",&k);
			for(int j=1;j<=k;++j){
				scanf("%d%d",&v,&w);
				InsertEdge(i,v,w);
			}
		}
		for(int i=n-m+1;i<=n;++i)scanf("%d",&val[i]);
		dfs(1,0);
		for(int i=m;i>=0;--i){
			if(temp[i]<=0){printf("%d\n",i);break;}
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: