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

TLE,用的背包DP???????????

Posted by BJ051155 at 2007-07-12 20:32:11 on Problem 3211
#include <iostream>
using namespace std;

int m,n,pieces[10],time[11][101],cost[101][100000];
char color[10][11];

struct{
	int time;
	char color[11];
}clothes[101];

int findcolor(int j)
{
	for(int i=1;i<=m;i++)
		if(!strcmp(color[i],clothes[j].color))
			return i;
	return 0;
}

int main(int argc, char* argv[])
{
	
	while(cin>>m>>n&&m+n)
	{
		memset(pieces,0,sizeof(pieces));
		for(int i=1;i<=m;i++)
			cin>>color[i];
		for(i=1;i<=n;i++)
		{
			cin>>clothes[i].time>>clothes[i].color;
			int t=findcolor(i);
			time[t][++pieces[t]]=clothes[i].time;
		}
		int total=0;
		for(i=1;i<=m;i++)
		{
			int w=0;
			for(int j=1;j<=pieces[i];j++)
				w+=time[i][j];
			memset(cost,0,sizeof(cost));
			for(j=1;j<=pieces[i];j++)
				for(int k=1;k<=w/2;k++)
				{
					cost[j][k]=cost[j-1][k];
					if(k>=time[i][j]&&cost[j-1][k-time[i][j]]+time[i][j]>cost[j][k])
						cost[j][k]=cost[j-1][k-time[i][j]]+time[i][j];
				}
			total+=w-cost[pieces[i]][w/2];
		}
		cout<<total<<endl;
	}
	return 0;
}

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