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 any logical answer, they only want as much as they
wish! Nasa knows quite well that they would 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 would never
let anyone have such amount of food that exceeds his ‘total happiness‘. He
planned that he would give someone even a fraction of a piece of item, but never
give anyone more than he needed!
He also decided that each 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 has an infinite amount of money and hence, he would 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
serious 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. 这是线性规划模版题。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 const double EPS = 1e-10; 6 const int MAXN = 55; 7 const int INF = 0x3fff3fff; 8 9 inline int sgn(double x) { 10 return (x>EPS)-(x<-EPS); 11 } 12 13 double A[MAXN][MAXN]; 14 double b[MAXN],c[MAXN]; 15 int N[MAXN],B[MAXN]; 16 int n,m; 17 double v; 18 19 bool init() { 20 N[0]=B[0]=v=0; 21 for(int i=1;i<=n;++i)N[++N[0]]=i; 22 for(int i=1;i<=m;++i)B[++B[0]]=n+i; 23 return true; 24 } 25 26 void pivot(int l,int e){ 27 b[e]=b[l]/A[l][e]; 28 A[e][l]=1.0/A[l][e]; 29 for(int i=1;i<=N[0];++i){ 30 int &x=N[i]; 31 if(x!=e)A[e][x]=A[l][x]/A[l][e]; 32 } 33 for(int i=1;i<=B[0];++i)if(B[i]!=){ 34 int y=B[i]; 35 b[y]-=A[y][e]*b[e]; 36 A[y][l]=-A[y][e]*A[e][l]; 37 for(int j=1;j<=N[0];++j){ 38 int x=N[j]; 39 if(x!=e)A[y][x]-=A[e][x]*A[y][e]; 40 } 41 } 42 v+=b[e]*c[e]; 43 c[l]=-A[e][l]*c[e]; 44 for(int i=1;i<=N[0];++i) { 45 int x=N[i]; 46 if(x!=e)c[x]-=A[e][x]*c[e]; 47 } 48 for(int i=1;i<=N[0];++i)if(N[i]==e)N[i]=l; 49 for(int i=1;i<=B[0];++i)if(B[i]==l)B[i]=e; 50 } 51 52 bool simplex() { 53 while(true) { 54 int e=MAXN; 55 for(int i=1;i<=N[0];++i) { 56 int x=N[i]; 57 if(sgn(c[x])>0&&x<e)e=x; 58 } 59 if(e==MAXN) break; 60 double delta=-1; 61 int l=MAXN; 62 for(int i=1;i<=B[0];++i) { 63 int y=B[i]; 64 if(sgn(A[y][e])>0){ 65 double tmp=b[y]/A[y][e]; 66 if(delta==-1||sgn(tmp-delta)<0||(sgn(tmp-delta)==0&&y<l)){ 67 delta=tmp; 68 l=y; 69 } 70 } 71 } 72 if(l==MAXN) return false; 73 pivot(l,e); 74 } 75 return true; 76 } 77 78 int main() { 79 while(scanf("%d%d",&n,&m)!=EOF) { 80 for(int i=1;i<=n;++i) 81 scanf("%lf",&c[i]); 82 for(int i=1;i<=m;++i){ 83 for(int j=1;j<=n;++j) 84 scanf("%lf",&A[n+i][j]); 85 scanf("%lf",&b[n+i]); 86 } 87 init(); 88 simplex(); 89 printf("Nasa can spend %d taka.\n",(int)ceil(v*m)); 90 } 91 }