POJ1155TELE(트리 DP)
3702 단어 dp
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
:dp[p][m]: p m user 。
dp[p][m]=max(dp[p][m],dp[p][m-k]+dp[son][k]-cost);
0, user 。
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
#define INF -9999999
typedef struct nnn
{
int id,cost;
}node;
vector<node>map[3005];
int dp[3005][3005],val[3005],k[3005],vist[3005],N,M;
int max(int a,int b)
{
return a>b?a:b;
}
void dfs(int p)
{
vist[p]=1; k[p]=0;
for(int i=0;i<=N;i++)
dp[p][i]=INF;
dp[p][0]=0;
if(p>N-M) dp[p][1]=val[p],k[p]=1;//
for(int i=0;i<map[p].size();i++)
{
int son=map[p][i].id;
if(vist[son])continue ;
dfs(son); k[p]+=k[son];
for(int m=k[p];m>=1;m--)
for(int tk=1;tk<=k[son];tk++)
dp[p][m]=max(dp[p][m],dp[p][m-tk]+dp[son][tk]-map[p][i].cost);
}
}
int main()
{
node p;
int id,cost,tk;
while(scanf("%d%d",&N,&M)>0)
{
for(int i=1;i<=N;i++)
map[i].clear(),vist[i]=0;
for(int i=1;i<=N;i++)
if(i<=N-M)
{
scanf("%d",&tk);
while(tk--)
{
scanf("%d%d",&id,&cost);
p.id=id; p.cost=cost; map[i].push_back(p);
p.id=i; p.cost=cost; map[id].push_back(p);
}
}
else{
scanf("%d",&val[i]);
}
dfs(1);
for(int i=N;i>=0;i--)
if(dp[1][i]>=0)
{
printf("%d
",i);break;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【경쟁 프로 전형적인 90문】008의 해설(python)의 해설 기사입니다. 해설의 이미지를 봐도 모르는 (이해력이 부족한) 것이 많이 있었으므로, 나중에 다시 풀었을 때에 확인할 수 있도록 정리했습니다. ※순차적으로, 모든 문제의 해설 기사를 들어갈 예정입니다. 문자열...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.