POJ 1010 STAMPS (DFS + 剪枝)

来源:互联网 发布:正版的办公软件 编辑:程序博客网 时间:2024/06/17 07:10


STAMPS

Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 16353
Accepted: 4635

Description

Have you done any Philately lately?

You have been hired by the Ruritanian Postal Service (RPS) to design their new postage software. The software allocates stamps to customers based on customer needs and the denominations that are currently in stock.

Ruritania is filled with people who correspond with stamp collectors. As a service to these people, the RPS asks that all stamp allocations have the maximum number of different types of stamps in it. In fact, the RPS has been known to issue several stamps of the same denomination in order to please customers (these count as different types, even though they are the same denomination). The maximum number of different types of stamps issued at any time is twenty-five.

To save money, the RPS would like to issue as few duplicate stamps as possible (given the constraint that they want to issue as many different types). Further, the RPS won't sell more than four stamps at a time.

Input

The input for your program will be pairs of positive integer sequences, consisting of two lines, alternating until end-of-file. The first sequence are the available values of stamps, while the second sequence is a series of customer requests. For example:

1 2 3 0 ; three different stamp types
7 4 0 ; two customers
1 1 0 ; a new set of stamps (two of the same type)
6 2 3 0 ; three customers

Note: the comments in this example are *not* part of the data file; data files contain only integers.

Output

For each customer, you should print the "best" combination that is exactly equal to the customer's needs, with a maximum of four stamps. If no such combination exists, print "none".
The "best" combination is defined as the maximum number of different stamp types. In case of a tie, the combination with the fewest total stamps is best. If still tied, the set with the highest single-value stamp is best. If there is still a tie, print "tie".

For the sample input file, the output should be:

7 (3): 1 1 2 3
4 (2): 1 3
6 ---- none
2 (2): 1 1
3 (2): tie

That is, you should print the customer request, the number of types sold and the actual stamps. In case of no legal allocation, the line should look like it does in the example, with four hyphens after a space. In the case of a tie, still print the number of types but do not print the allocation (again, as in the example).Don't print extra blank at the end of each line.

Sample Input

1 2 3 0; three different stamp types7 4 0; two customers1 1 0; a new set of stamps (two of the same type)6 2 3 0; three customers

Sample Output

7 (3): 1 1 2 3 4 (2): 1 3 6 ---- none2 (2): 1 13 (2): tie

Source

Pacific Northwest 1998


题目链接:http://poj.org/problem?id=1010

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

题目分析:题意限制条件很多啊,dfs的时候根据条件更新就可以了,详细见代码


#include <cstdio>#include <algorithm>using namespace std;//不同种类的面值,先前最佳解,当前解int kind[100], pre[4], cur[4];//种类,需求,先前已存在最佳解的种类数、张数、单张最大面值int type, need, pKind, pNum, pVal;//最佳方案数int ans;//当前可以购买的种类,上次购买的种类,当前购买的种类数、张数、单张最大面值void DFS(int k, int lKind, int cKind, int cNum, int cVal, int cost){    if(cNum > 4 || (cNum == 4 && cost != need))         return;    if(cost == need) //可行方案    {        if( (pKind == 0) || //先前没有可行解            (cKind > pKind) ||  //种类多            (cKind == pKind && cNum < pNum) || //张数少            (cKind == pKind && cNum == pNum && cVal > cVal)) //面值大        {            pKind = cKind;            pNum = cNum;            pVal = cVal;            for(int i = 0; i < cNum; i++)                pre[i] = cur[i];            ans = 1;        }        else if(cKind == pKind && cNum == pNum && cVal == pVal) //存在多种最佳方案            ans ++;        return;    }    for(int i = k; i < type; i++)    {        if(cost + kind[i] > need) //排过序            break;        int tKind = cKind, tVal = cVal;        if(lKind != i)             tKind = cKind + 1;        if(cVal < kind[i])             tVal = kind[i];        cur[cNum] = kind[i];        DFS(i, i, tKind, cNum + 1, tVal, cost + kind[i]);    }}int main(){    while(scanf("%d", &kind[0]) != EOF)    {        type = 0;        while(kind[type] != 0)            scanf("%d", &kind[++type]);        sort(kind, kind + type);        while(scanf("%d", &need) && need)        {            pKind = pNum = pVal = 0;            ans = 1;            DFS(0, -1, 0, 0, 0, 0);            if(pKind == 0)                printf("%d ---- none\n", need);            else if(ans > 1)                printf("%d (%d): tie\n", need, pKind);            else            {                printf("%d (%d): ", need, pKind);                for(int i = 0; i < pNum - 1; i++)                    printf("%d ", pre[i]);                printf("%d\n", pre[pNum - 1]);            }        }    }}


0 0