UVa 10498 Happiness! (线性规划)

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 buyn different items. He then asked each of them 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.

Problemsetter: Mustaq Ahmed, University of Waterloo

 

/*标准的线性规划---直接求即可。(注意:贪心优化可以大大提高效率。)*/

 

 

 



#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;//原线性规划问题中的变量个数,即:松弛变量前的变量个数 */
int r;


/*const int m=3;//先行约束条件个数 */
int m;


const double eps=1e-6;


const double Max=1<<30;
 
double matrix[50][50];//单纯性算法的表格 




/*
int flag;
flag = = 0 , 表示目标函数的最大值无边界,一般认为无解;
flag = = 1,  表示有唯一的最优基本可行解
flag = = 2, 表示目标函数值确定,但最优的基本可行解不唯一
*/




int N,M,ans;




int main()
{
    //有N种食品,M个人,就是N个变量,M个约束的线性方程 
    while(~scanf("%d%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];  //系数取反,填入单纯性算法表格
      }
      
      matrix[m+1][r+1]=0.0; //目标函数值初始为0
       
    //输入系数矩阵 
      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;
      //寻找非基本变量,一点点优化:选择尽可能小的基本变量,可以减少迭代次数(贪心思想) 
        int ru;
        for(int i=1;i<=r+1;i++){
          if(matrix[m+1][i]<Min){
            Min=matrix[m+1][i];
            ru=i;
          }
        }
        
      //若找不到合适的非基本变量,说明已经找到解了(本题不存在无解情况) 
        if(Min==0.0) break;
      
        Min=Max;
      //寻找基本变量
        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;
            }
          }
        }
      
      
      //先修改表格matrix[chu][ru],修改主元 
        matrix[chu][ru]=1.0/matrix[chu][ru];
      
      //修改表格的主元行
        for(int j=1;j<=r+1;j++){
          if(j==ru) continue;
          matrix[chu][j]*=matrix[chu][ru];
        }
      
      //修改表格中其他单元格
        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];
          }
        }
      
      //修改表格中主元列
        for(int j=1;j<=m+1;j++){
          if(j==chu) continue;
          matrix[j][ru]=-matrix[j][ru]*matrix[chu][ru];
        }
      }
      
      
    //求最大花销,即每个人的最优解 * 人数,题目要求向上取整。。。。 
      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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值