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

囧一个!顺便赋个代码!

Posted by nothing828 at 2011-03-22 22:48:53 on Problem 1010
就因为没有注释掉
      freopen("in.txt","r",stdin);
     freopen("out.txt","w",stdout);
WA了一个小时,鄙视一下自己

题意:
  有多种类型的邮票,每种类型有特定的面值(不同类型面值可以相同)。
给出每个客户所需的面值总和,客户的需求为:使购买邮票的总面值为所
求面值总和。若不存在满足需求的方案,输出none;否则,输出最佳方案。
  最佳方案的定义如下(优先级递减):
  1.种类最多
  2.张数最少
  3.单张面值最大
  经过上述约束不能找出最佳方案,输出tie
  其中,每位顾客购买邮票张数不超过4

#include<iostream>
#include<algorithm>
using namespace std;

//不同种类的面值,先前最佳可行解,当前解
int kind[100],pre[4],cur[4];
//种类,需求,先前已存在最佳解的种类数、张数、单张最大面值
int types,need,PreKind,PreNum,PreVal;
//最佳方案的个数
int TieNum;

//当前可以购买的种类,上次购买的种类,当前购买的种类数、张数、单张最大面值
void DFS(int k,int LastGetKind,int CurKind,int CurNum,int CurVal,int cost)
{
	if(CurNum>4 || (CurNum==4 && cost!=need)) return ;
	if(cost==need) //可行方案
	{
		if( (PreKind==0) || /*1.先前没有可行解*/
			(CurKind>PreKind) ||  /*2.种类多*/
			(CurKind==PreKind && CurNum<PreNum) || /*3.张数少*/
			(CurKind==PreKind && CurNum==PreNum && CurVal>PreVal)) /*4.面值大*/
		{
			PreKind=CurKind;
			PreNum=CurNum;
			PreVal=CurVal;
			for(int i=0;i<CurNum;i++)
				pre[i]=cur[i];
			TieNum=1;
		}
		else if(CurKind==PreKind && CurNum==PreNum && CurVal==PreVal) //存在多种最佳方案
			TieNum++;
		return ;
	}
	for(int i=k;i<types;i++)
	{
		if(cost+kind[i]>need) break;
		int KIND=CurKind,VAL=CurVal;
		if(LastGetKind!=i) KIND=CurKind+1;
		if(CurVal<kind[i]) VAL=kind[i];
		cur[CurNum]=kind[i];
		DFS(i,i,KIND,CurNum+1,VAL,cost+kind[i]);
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	while(scanf("%d",&kind[0])!=EOF)
	{
		types=0;
		while(kind[types]!=0)
			scanf("%d",&kind[++types]);
		//===============================
		//for(int i=0;i<types;i++)
		//	printf("%d ",kind[i]);
		//printf("\n");
		//===============================
		//按面值从小到大排序
		sort(kind,kind+types);
		while(scanf("%d",&need) && need)
		{
			PreKind=PreNum=PreVal=0;
			TieNum=1;
			DFS(0,-1,0,0,0,0);
			if(PreKind==0)
				printf("%d ---- none\n",need);
			else if(TieNum>1)
				printf("%d (%d): tie\n",need,PreKind);
			else
			{
				printf("%d (%d): ",need,PreKind);
				for(int i=0;i<PreNum-1;i++)
					printf("%d ",pre[i]);
				printf("%d\n",pre[PreNum-1]);
			}
		}
	}
	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