Title Link: http://acm.swust.edu.cn/problem/465/
Time limit (ms): $ Memory Limit (KB): 65535 description Wu Grandma has a lovely granddaughter--Kiki, she likes small animals, especially fish. In order to let the small granddaughter to raise the beautiful small fish, Wu grandmother early in the morning to the birds and flowers insects market to buy fish. This market is really big, there are all kinds of pets, even pet fish have dozens of kinds. These fish are too beautiful, buy more and more people, but because the supply is limited, the owner of the sale of fish have to rule: the same fish, everyone can only buy one, and some fish can not be bought together, because they will fight each other to swallow.
Wu Grandma wants to buy as many fish as possible, but unfortunately, her funds are limited, what can be done? Please write a program to help her. If you have more than one plan to buy as many fish as possible, choose the one that spends the most money. The first behavior of the input file is two positive integers M (m≤1000), N (n≤30), respectively, representing the funds and types of fish of Wu's grandmother. The following N lines, each with two positive integers S (1≤s≤n), T, indicate the number of a fish and the price of the fishes.
Next, there are two integer p,q per line. When P,Q is greater than 0 o'clock, p,q cannot coexist; when p,q equals 0 o'clock, the end of the input file is indicated. The first behavior of the output file is two positive integers, x, y, respectively, representing the number of fish purchased and the total cost. The following x lines, each with an integer, indicate the number of the fish to be bought. Numbering sorts the output in ascending order.
If the topic has multiple solutions, simply output one of them. Sample Input
170 71 702 503 304 405 406 307 201 41 73 43 55 76 70 0 |
Sample Output
Hint
Problem -Solving ideas: This topic to remove the fish between each other to swallow, and the output of the fish purchased, is a 0-1 knapsack problems.
now on the basis of 0-1 backpack to achieve the above two points to design a DFS algorithm, recursive simulation of the process, in the code has detailed comments, here is not much to say ~ ~ ~The code is as follows:
1 /******************0-1 Backpack +dfs******************/2#include <iostream>3 using namespacestd;4 5 #defineRep (i,a,b) for (int i=a;i<=b;i++)6 7 intbuy[ to], sign[ to];//sign flag fish purchase status, buy Best Buy solution8 intmpt[ to][ to];//indicates the state of mutual restraint of fish9 intVal, num, N, M, vi[ to];//VI PriceTen One voidDfsintCurintSumintCNT) { A intptr[ to];//Transit Variables - if(Cur >N) { - if(num<cnt | | (num = = cnt&&sum>val)) { thenum =CNT; -val =sum; -Rep (I,1, n) buy[i] =Sign[i]; - } + return; - } + if(!sign[cur] && sum + vi[cur] <=m) { ARep (I,1, n) ptr[i] =Sign[i]; atSign[cur] =2;//Mark this fish has been purchased -Rep (I,1, N) { - if(Mpt[cur][i] &&!Sign[i]) -Sign[i] =1; - } -DFS (cur +1, Sum + vi[cur], CNT +1); in //Restore the purchase status of fish to facilitate next search -Rep (I,1, n) sign[i] =Ptr[i]; toSign[cur] =0; + } -DFS (cur +1, sum, CNT); the } * intMain () { $ intA, B;Panax NotoginsengCIN >> M >>N; - //fish do not necessarily give in order, Pit Daddy ~ ~ theRep (I,1, N) { +CIN >>A; ACIN >>Vi[a]; the } +Cin >> a >>b; - while(A | |b) { $MPT[A][B] = Mpt[b][a] =1; $Cin >> a >>b; - } -Dfs1,0,0); thecout << Num <<' '<< Val <<Endl; -Rep (I,1, N) {Wuyi if(Buy[i] = =2) thecout << i <<Endl; - } Wu return 0; -}
View Code
[Swust OJ 465]--wu granny buys fish (0-1 backpack +dfs)