Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

AC 疏星背包的时候要从后往前更新,否则一个点会被一个儿子更新两遍

Posted by PoPoQQQ at 2014-04-24 18:19:22 on Problem 2486
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define s son[x][i]
typedef struct abcd{int to;abcd*next;} abcd;abcd*head[110];
void add(int x,int y){abcd*temp=(abcd*)malloc(sizeof(abcd));temp->to=y;temp->next=head[x];head[x]=temp;}
int max(int x,int y){return x>y?x:y;}
int f[110][210][2];//f[i][j][0]=x表示从i点开始走j步回到i点能吃到最多苹果,1是不回来; 
int a[110],fa[110],son[110][110],mark[110];
int ans;
int n,k;
void build_tree(int x)
{
	abcd*temp;
	mark[x]=1;
	for(temp=head[x];temp;temp=temp->next)if(!mark[temp->to])build_tree(temp->to),fa[temp->to]=x,son[x][++son[x][0]]=temp->to;
}
void Treedp(int x)
{
	int i,j,l;
	f[x][0][0]=a[x];
	for(i=1;s;i++)
	{
		Treedp(s);
		for(j=k;j>=0;j--)for(l=k;l>=0;l--)
		{
			if(j+l>=k)continue;
			f[x][j+l+1][1]=max(f[x][j+l+1][1],f[x][j][0]+f[s][l][1]);
			if(j+l+2>k)continue;
			f[x][j+l+2][0]=max(f[x][j+l+2][0],f[x][j][0]+f[s][l][0]);
			f[x][j+l+2][1]=max(f[x][j+l+2][1],f[x][j][1]+f[s][l][0]);
		}
	}
	f[x][0][1]=a[x];
}
int main()
{
	int i,j,x,y;
	while(scanf("%d",&n)!=EOF)
	{
		memset(head,0,sizeof(head));
		memset(f,0xef,sizeof(f));
		memset(son,0,sizeof(son));
		memset(mark,0,sizeof(mark));
		ans=0;
		scanf("%d",&k);
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		for(i=1;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x);
		build_tree(1);
		Treedp(1);
		for(i=0;i<=k;i++)ans=max(ans,max(f[1][i][0],f[1][i][1]));
		printf("%d\n",ans);
	}
}

C的代码,略原始,凑活看吧

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator