Dynamic Planning-Gold Mine Model
Problem description:
There are people and num gold mines. I _People people are required to mine Each Gold Mine. I _GetGold can be obtained, and those who have used it cannot reuse it, ask how much gold can be obtained from this num gold mine at most;
Input
There are two numbers in the first line. The first is the total number of gold mines and the second is the total number of gold mines.
In the input file, rows 2nd to n + 1 have two numbers in each row, and the two numbers in row I represent the number of gold miners in the I-1 and the number of gold available.
Output
Returns an integer to indicate the maximum number of gold.
Input sample;
100 5
77 92
22 22
29 87
50 46
99 90
Output example
133
Thought Source: click here
The C language code is provided below:
# Include
# Include
# Define max_people 10000 // maximum number of people supported by the Program # define max_gold 100 // maximum number of gold mines supported by the program int Total_People; // The total number of people that can be used to mine mines int Total_Gold; // total number of gold mines int I _People [100]; // number of mines required for the I-th Gold Mine, the I-th Gold Mine subscript I-1 in the program indicates int I _GetGold [100]; // The number of gold that can be obtained by digging the I-th Gold Mine. int max_Gold [max_people] [max_gold]; // max [I] [j] stores the maximum number of gold that I can obtain from the first j gold mines (0 -- J-1, -1 indicates unknown // data initialization int max (int a, int B) {return a> B? A: B;} void GetData () {scanf ("% d", & Total_People, & Total_Gold); // total number of reads, and the number of gold mines for (int I = 0; I <Total_Gold; I ++) {scanf ("% d", & I _People [I], & I _GetGold [I]); // read the number of users required for the I-th gold mine and the number of gold available for mining} memset (max_Gold,-1, sizeof (max_Gold )); // assign all values to the array as-1 Based on the header file
} // Obtain the maximum gold count int GetMaxGold (int people, int num) obtained by the num gold mine before the mining by people. // It indicates the number of available people, num is the number of gold mines that can be mined {int MaxGold; if (max_Gold [people] [num]! =-1) // if this problem has been recorded, that is, the "Memorandum" {MaxGold = max_Gold [people] [num];} else if (num = 0) in the dynamic plan) // if only one gold mine can be mined, that is, the "boundary condition" for Dynamic Planning {if (people> = I _People [num]) // Number of people meeting the needs of mining the gold mine MaxGold = I _GetGold [num]; else // number of people not meeting the needs of mining the gold mine MaxGold = 0 ;} else if (people> = I _People [num]) // if a person from people needs to mine the gold mine, two conditions are considered (whether the mine is mined ), take the maximum value {MaxGold = max (GetMaxGold (people-I _People [num], num-1) + I _GetGold [num], GetMaxGold (people, num-1 ));} else // if an individual does not meet the requirements for mining the gold mine, MaxGold = GetMaxGold (people, num-1) is considered only ); max_Gold [people] [num] = MaxGold; // records the maximum number of gold currently. In the dynamic plan, return MaxGold;} int main () {GetData (); // read data printf ("% d \ n for multiple pairs", GetMaxGold (Total_People, Total_Gold); return 0 ;}