POJ 1874 trade on Verweggistan

Source: Internet
Author: User
Tags printf

Trade on Verweggistan

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1238 Accepted: 355

Description Since the days of Peter Stuyvesant and Abel Tasman, Dutch merchants has been traveling all over the world to Buy and sell goods. Once There was some trader on Verweggistan, but it ended after a short time. After reading this story, you'll understand why.
At this time Verweggistan was quite popular, because it is the only place in the world where people knew what to make a ' p Rul '. The end of the trade on Verweggistan meant the end of the trade in Pruls (or ' Prullen ', as the Dutch Plural said), and Ver Y few people nowadays know what a prul actually are.

Pruls were manufactured in Workyards. Whenever a Prul was finished it is packed in a box, which is then placed on top of the pile of previously produced pruls . The side of each box is written. The price of depended on the time it took to manufacture the Prul. If all went well, a prul would cost one or both florins, but on a bad day the price could easily rise to florins or more . This had-do with quality; All pruls had the same value.

In those days Pruls sold-florins each in Holland. Transportation costs were negligible since the pruls were taken as extra on ships that would sail anyway. When a Dutch merchant went to Verweggistan, he had a clear purpose:buy pruls, sell them in Holland, and maximize his Prof Its. Unfortunately, the Verweggistan to trading pruls made this more complicated than one would think.

One would expect that merchants would simply buy the cheapest pruls, and the pruls so cost more than florins would re Main unsold. Unfortunately, all workyards on Verweggistan sold their pruls in a particular order. The box on top of the pile is sold first, then the second one from the top, and so on. So even if the fifth box is from the top is the cheapest one, a merchant would has to buy the other four boxes above to OBT Ain it.

As can imagine, this made it quite difficult for the merchants-maximize their profits by buying the right set of PR Uls. Not have computers to help with optimization, they quickly lost interest in trading pruls at all.

In this problem, given the description of several workyard piles. Calculate the maximum profit a merchant can obtain by buying pruls from the piles according to the restriction s given above. In addition, you had to determine the number of pruls he had to buy to achieve this profit.

Input the input describes several test cases. The first line of input is a contains a single integer w and the number of workyards in the test case (1 <= W <= 50).
This was followed by W lines and each describing a pile of pruls. The first number in each line is the number B of boxes in the pile (0 <= b <= 20). Following it is b positive integers, indicating the prices (in florins) of the pruls in the stacks, given from top to Bott Om.

The input is terminated by a description starting with W = 0. This description should is processed.

Print the case number (1, 2, ...) in the Output for each test case. Then print lines, the first containing the maximum profit the merchant can achieve. The second line should specify the number of Pruls the merchant have to buy to obtain this profit. If This number isn't uniquely determined, print the possible values in increasing order. If There is more than ten possible values, print is only the ten smallest.
Display a blank line between test cases.

Sample Input

1 6 3 7 5 2 5 7 3 One 9 9 1 2
3 4 Ten
4 + 0

Sample Output

Workyards 1
Maximum profit is 8.
Number of Pruls to Buy:4

workyards 2
Maximum profit is.
Number of Pruls to Buy:6 7 8 9 10 12 13

Source World Finals 1999

/*
The application of knapsack problem, processing each yard in turn and then looking for the number of feasible scheme to reach the maximum value,
And then forward the backpack.
*/
#include <iostream> #include <algorithm> #define MAX_B #define MAX_W using namespace std; struct pile {int num; int Choice[max_b + 5];} Piles[max_w + 5]; int Pnum, Maxprofit; int Res[max_b * Max_w + 5], resnum; int Temp[max_b * max_w + 5]; BOOL V[max_w * Max_b + 5]; BOOL Compare (const int &AMP;V1, const int &v2) {return V1 <= v2;} int main () {int p, I, j, seq = 0; while (scanf ( "%d", &pnum) && Pnum! = 0) {seq++; maxprofit = 0; memset (piles, 0, sizeof (piles)); resnum = 1; Res[resnum] = 0; V[0] = true; int PPOs = 0; for (p = 1; p <= pnum; p++) {ppos++; Int. Bnum, price, MaxPro = 0, Curpro = 0; piles[p].num = 1; piles[p].choice[1] = 0; scanf ("%d", &bnum); for (i = 1; I <= bnum; i++) {scanf ("%d", &price); Curpro + = 10-price; if (Curpro > MaxPro) {piles[ppos].num = 1; Piles[ppos].choice[piles[ppos].num] = i; MaxPro = Curpro; } else if (Curpro = = MaxPro) {piles[ppos].num++; Piles[ppos].choice[piles[ppos].num] = i;}} Maxprofit + = maxPro; int newSize = 0; memset (V, 0, sizeof (v)); for (i = 1, i <= piles[p].num; i++) {for (j = 1; J <= Resnum; j + +) {int newval = Piles[p].choice[i] + res[j]; if (!v[ NewVal]) {V[newval] = true; Temp[++newsize] = newval;}} } resnum = NewSize; for (i = 1; I <= newSize; i++) res[i] = Temp[i]; } sort (&res[1], &res[1] + resnum, compare); if (Resnum >) resnum = 10; printf ("Workyards%d/n", seq); printf ("Maximum profit is%d./n", maxprofit); printf ("Number of pruls to buy:"); for (i = 1; I <= resnum; i++) printf ("%d", res[i]); printf ("/n/n"); } return 0; } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.