Problem G
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 buy n different items. He then asked each of the m contestents How much of each item they want to eat. They could not give no logical answer, they only want as much as they wish! Nasa knows quite well, they would only waste their food if they get as much as they want. He was determined the not-let-that happen.
So he tactfully found off 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. The It May is the case that someone gets ' zero ' happiness ' on some item (s). He decided that he would never let anyone has such amount of food that exceeds his ' total happiness '. He planned that he would give someone even a fraction of a piece of the item, but never give anyone more than he needed!
He also decided that all would 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 had a infinite amount of money and hence, he would spend as much mo Ney as he can.
Input
Input contains data collected by Nasa in 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 is ' happiness ' got from each item a nd the last one was the ' total happiness '.
Output
For the data collected in each day, print in a, the maximum amount of money Nasa can spend in Taka rounded Nearest integer. You can assume that there would be no such input which may causeserious 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.
This is a linear programming template problem.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cmath>5 Const DoubleEPS = 1e-Ten;6 Const intMAXN = -;7 Const intINF =0x3fff3fff;8 9InlineintSgnDoublex) {Ten return(x>eps)-(x<-EPS); One } A - DoubleA[MAXN][MAXN]; - DoubleB[MAXN],C[MAXN]; the intN[MAXN],B[MAXN]; - intn,m; - Doublev; - + BOOLinit () { -n[0]=b[0]=v=0; + for(intI=1; i<=n;++i) n[++n[0]]=i; A for(intI=1; i<=m;++i) b[++b[0]]=n+i; at return true; - } - - voidPivotintLinte) { -b[e]=b[l]/A[l][e]; -a[e][l]=1.0/A[l][e]; in for(intI=1; i<=n[0];++i) { - int&x=N[i]; to if(x!=e) a[e][x]=a[l][x]/A[l][e]; + } - for(intI=1; i<=b[0];++i)if(b[i]!=){ the inty=B[i]; *b[y]-=a[y][e]*B[e]; $a[y][l]=-a[y][e]*A[e][l];Panax Notoginseng for(intj=1; j<=n[0];++j) { - intx=N[j]; the if(x!=e) a[y][x]-=a[e][x]*A[y][e]; + } A } thev+=b[e]*C[e]; +c[l]=-a[e][l]*C[e]; - for(intI=1; i<=n[0];++i) { $ intx=N[i]; $ if(x!=e) c[x]-=a[e][x]*C[e]; - } - for(intI=1; i<=n[0];++i)if(n[i]==e) n[i]=l; the for(intI=1; i<=b[0];++i)if(b[i]==l) b[i]=e; - }Wuyi the BOOLsimplex () { - while(true) { Wu intE=MAXN; - for(intI=1; i<=n[0];++i) { About intx=N[i]; $ if(SGN (c[x) >0&&x<e) e=x; - } - if(E==MAXN) Break; - Doubledelta=-1; A intL=MAXN; + for(intI=1; i<=b[0];++i) { the inty=B[i]; - if(SGN (A[y][e) >0){ $ Doubletmp=b[y]/A[y][e]; the if(delta==-1|| SGN (Tmp-delta) <0|| (SGN (tmp-delta) = =0&&y<l)) { theDelta=tmp; theL=y; the } - } in } the if(L==MAXN)return false; the pivot (l,e); About } the return true; the } the + intMain () { - while(SCANF ("%d%d", &n,&m)! =EOF) { the for(intI=1; i<=n;++i)Bayiscanf"%LF",&c[i]); the for(intI=1; i<=m;++i) { the for(intj=1; j<=n;++j) -scanf"%LF", &a[n+i] [j]); -scanf"%LF", &b[n+i]); the } the init (); the simplex (); theprintf"Nasa can spend%d taka.\n",(int) Ceil (v*m)); - } the}
Mathematics (Linear programming): Uvaoj 10498 Happiness