Ultraviolet A 10498 happiness! (Linear Programming)

Source: Internet
Author: User

Happiness!

Input:Standard Input
Output:Standard output
Time limit:3 seconds

Prof. kaykobad has given NASA the duty of buying some food for the ACM contestents. NASA decided to buyNDifferent items. He then asked each ofMContestents how much of each item they want to eat. They cocould not give
Any logical answer, they only want as much as they wish! NASA knows quite well that they wowould only waste their food if they get as much as they want. He was determined not to let that happen.

So He tactfully found out from each of the contestents how much 'Happiness 'one gets from each piece of each item and what is the 'total happiness' over which one wastes food. it may be the case that someone gets 'zero ''' happiness 'on some item (s ). he decided
That he wowould never let anyone have such amount of food that exceeds his 'total happiness '. he planned that he wocould give someone even a fraction of a piece of item, but never give anyone more than he needed!

He also decided that each wocould get exactly the same amount of each item so that no one can complain against him.

After planning all these, he finally realized that he has an infinite amount of money and hence, he wowould spend as much money as he can.

Input

Input contains data collected by NASA on several days.

For each day,

The first line contains the integers n (3 <= n <= 20) and M (3 <= m <= 20 ).

The next line contains n real numbers, the per unit price of each item.

Each of the next M lines contain data (n + 1 real numbers) of each contestents: First N are 'Happiness 'got from each item and the last one is the 'total happiness '.

Output

For the data collected in each day print in a single line the maximum amount of money NASA can spend in Taka rounded up to nearest integer. you can assume that there will be no such input which may cause serous floating point errors.

Sample Input
3 3
1 0.67 1.67
1 2 1 430
3 0 2 460
1 4 0 420
Sample output
Nasa can spend 1354 taka.

Problemsetter: mustaq Ahmed, University of Waterloo

 

/* Standard linear planning-simply find it. (Note: greedy optimization can greatly improve efficiency .) */

 

 

 

# Include <functional>
# Include <algorithm>
# Include <iostream>
# Include <fstream>
# Include <sstream>
# Include <iomanip>
# Include <numeric>
# Include <cstring>
# Include <cassert>
# Include <cstdio>
# Include <string>
# Include <vector>
# Include <bitset>
# Include <queue>
# Include <stack>
# Include <cmath>
# Include <ctime>
# Include <list>
# Include <set>
# Include <map>

Using namespace STD;

/* Const int r = 2; // number of variables in the original linear programming problem, that is, the number of variables before the variables are relaxed */
Int R;

/* Const int M = 3; // Number of preemptible constraints */
Int m;

Const double EPS = 1e-6;

Const double max = 1 <30;
 
Double matrix [50] [50]; // table of simple algorithms

/*
Int flag;
Flag = 0, indicating that the maximum value of the target function has no boundary. Generally, it is considered that there is no solution;
Flag = 1, indicating that there is a unique optimal Basic Feasible Solution
Flag = 2, indicating that the target function value is determined, but the optimal basic feasible solution is not unique.
*/

Int n, m, ans;

Int main ()
{
// There are n kinds of foods, m Individual, that is, n variables, linear equations with M Constraints
While (~ Scanf ("% d", & N, & M )){
R = N;
M = m;
For (INT I = 1; I <= r; I ++ ){
Scanf ("% lf", & matrix [M + 1] [I]);
Matrix [M + 1] [I] =-matrix [M + 1] [I]; // returns the coefficient and fills in the simple algorithm table.
}

Matrix [M + 1] [R + 1] = 0.0; // the initial value of the target function is 0.

// Input Coefficient Matrix
For (INT I = 1; I <= m; I ++)
For (Int J = 1; j <= R + 1; j ++)
Scanf ("% lf", & matrix [I] [J]);


While (1 ){
Double min;
Min = 0.0;
// Search for non-basic variables and optimize them a little bit: select as few basic variables as possible to reduce the number of iterations (Greedy idea)
Int Ru;
For (INT I = 1; I <= R + 1; I ++ ){
If (Matrix [M + 1] [I] <min ){
Min = matrix [M + 1] [I];
Ru = I;
}
}

// If no suitable non-basic variable is found, it indicates that the solution has been found (this question does not exist)
If (min = 0.0) break;

Min = max;
// Search for basic variables
Int Chu;
For (Int J = 1; j <= m; j ++ ){
If (Matrix [J] [Ru]> 0 ){
Double TMP = matrix [J] [R + 1]/matrix [J] [Ru];
If (TMP <min ){
Min = TMP;
Chu = J;
}
}
}


// Modify the matrix [Chu] [Ru] and principal element first.
Matrix [Chu] [Ru] = 1.0/matrix [Chu] [Ru];

// Modify the principal row of the table
For (Int J = 1; j <= R + 1; j ++ ){
If (j = Ru) continue;
Matrix [Chu] [J] * = matrix [Chu] [Ru];
}

// Modify other cells in the table
For (INT I = 1; I <= m + 1; I ++ ){
If (I = Chu) continue;
For (Int J = 1; j <= R + 1; j ++ ){
If (j = Ru) continue;
Matrix [I] [J]-= matrix [Chu] [J] * matrix [I] [Ru];
}
}

// Modify the principal element column in the table
For (Int J = 1; j <= m + 1; j ++ ){
If (j = Chu) continue;
Matrix [J] [Ru] =-matrix [J] [Ru] * matrix [Chu] [Ru];
}
}


// Calculate the maximum cost, that is, the optimal number of people * for each person. The question must be rounded up ....
Ans = (INT) (Matrix [M + 1] [R + 1] * m );
If (Matrix [M + 1] [R + 1] * m> ans)
Ans ++;
Printf ("NASA can spend % d Taka. \ n", ANS );
}
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.