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 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.

将n种食品分给m个参赛选手,一个单位的某食品给某个选手一定的满足度,每个选手有一个最大满足度。为了避免浪费,分给每一个选手的食品都不超越选手的满足度。已知各种食品的单价,求最多可以花多少钱。

以样例输入为例:

输入第一行,有3中食品,3个选手。

第二行是每种食品的单价。目标函数求最多可以花多少钱,也就是

C=x1*1+x2*0.67+x3*1.67

输入后面某个选手,每种食品单位给予的满足度以及他最大满足度。也就是我们的约束条件:

x1*1+x2*2+x3*1<=430

x1*3+x2*0+x3*2<=460

x1*1+x2*4+x3*0<=420

在上面的分析容易知道,题目给出的就是我们需要的标准形式,不需要转换。所以直接建立单纯性算法矩阵就可以求解。

#include<stdio.h>
#define inf 0x3f3f3f
double g[57][57];//单纯性算法表格
int main()
{
    int n,m;//n种食品,m个人,就是n个变量,m个约束的线性方程
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        double min,tmp;
        int base,unbase,ans;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&g[m+1][i]);//先输入目标函数
            g[m+1][i]=-g[m+1][i];//系数取反
        }
        g[m+1][n+1]=0.0;//目标函数值初始为0
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n+1;j++)
                scanf("%lf",&g[i][j]);//输入系数矩阵
        while(true)
        {
            min=0.0;//选择非基本变量,一点点优化:选择尽可能小的基本变量,可以减少迭代次数
            for(int i=1;i<=n+1;i++)
                if(g[m+1][i]<min)
                {
                    min=g[m+1][i];
                    unbase=i;
                }
            if(min==0.0)break;//没有找到非基本变量,也就是已经找到解了
            //选择基本变量
            min=inf;
            for(int i=1;i<=m;i++)
                if(g[i][unbase]>0)
                {
                    tmp=g[i][n+1]/g[i][unbase];
                    if(min>tmp)
                    {
                        min=tmp;
                        base=i;
                    }
                }
            //因为题目有解,此处不需要判断是否找到了基本变量
            g[base][unbase]=1.0/g[base][unbase];//修改主元
            //修改主元行
            for(int i=1;i<=n+1;i++)
            {
                if(i==unbase)continue;
                g[base][i]*=g[base][unbase];
            }
            //修改其他元素
            for(int i=1;i<=m+1;i++)
            {
                if(i==base)continue;
                for(int j=1;j<=n+1;j++)
                {
                    if(j==unbase)continue;
                    g[i][j]-=g[base][j]*g[i][unbase];
                }
            }
            //修改主元列
            for(int i=1;i<=m+1;i++)
            {
                if(i==base)continue;
                g[i][unbase]=-g[i][unbase]*g[base][unbase];
            }
        }
        //求最大花销,即每个人的最优解乘以人数
        ans=(int)(g[m+1][n+1]*m);
        if(g[m+1][n+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、付费专栏及课程。

余额充值