列表

详情


NC223592. CCheese,IfYouPlease

描述

The “We Cut The Cheese” specialty food store sells specialized blendings of cheeses. For example, their Italian Blend is made up of 50% provolone, 30% mozzarella and 20% parmesan, while their African Safari is made up of 74% domiati, 25% areesh with just a hint (1%) of bokmakiri. You started working at the store as a cheese blend taster, but two years and 45 pounds later you have worked your way up to head of the accounting office. Every week the store gets various shipments of cheeses, depending on the time of year, market price and other factors. Given these amounts and the percentages required for each cheese blend, you are asked every week to determine the optimal use of these cheeses to maximize profit. When the store just made a few different cheese blends this could be done by hand, but with business expanding faster than a cheese souffle, the number of blends has also grown to the point where a program is now needed to determine the optimal use of the cheese shipments. So the question is: Are you gouda-nough to write such a program?

输入描述

Input starts with a line containing two positive integers n m (1 ≤ n, m ≤ 50), where n is the number of types of cheese used to make the cheese blends and m is the number of different cheese blends offered by the store. The next line contains n integers w1 w2 . . . wn (0 ≤ wi ≤ 500), where wi is the number of pounds of cheese type i that the store has on-hand. Following this are m lines of the form p1 p2 p3 . . . pn t (0.0 ≤ pi ≤ 100.0, 0.0 ≤ t ≤ 10.0), where pi indicates the percentage of cheese i found in the blend, and t is the profit per pound for the blend. 

输出描述

Output the maximum profit that can be obtained for the given pounds of cheese, blending percentages and profits, assuming all of the blended cheeses get sold. Round your answer to the nearest penny. 

示例1

输入:

3 2
100 150 100
50.0 50.0 0.0 3.20
0.0 50.0 50.0 2.80

输出:

920.00

示例2

输入:

3 2
100 150 100
50.0 50.0 0.0 3.20
0.0 40.0 60.0 2.80

输出:

1000.00

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 3ms, 内存消耗: 476K, 提交时间: 2021-07-29 09:55:24

#include<bits/stdc++.h>
using namespace std;
const int N=60;
const double eps=1E-8;
double a[N][N];
int main()
{
    int n,m;
    scanf("%d%d",&m,&n);
    for(int j=0;j<m;++j)scanf("%lf",a[0]+j);
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<=m;++j)
        {
            scanf("%lf",a[i]+j);
        }
    }
    while(1)
    {
        int u=1;
        while(u<=n&&a[u][m]<eps)++u;
        if(u>n)break;
        int v;
        double tmp=1E10;
        for(int j=0;j<m;++j)
        {
            if(a[u][j]<eps)continue;
            double t=a[0][j]/a[u][j];
            if(t<tmp)
            {
                v=j;
                tmp=t;
            }
        }
        for(int i=0;i<=n;++i)
        {
            if(i==u)continue;
            a[i][v]/=a[u][v];
            for(int j=0;j<=m;++j)
            {
                if(j==v)continue;
                a[i][j]-=a[u][j]*a[i][v];
            }
        }
        for(int j=0;j<=m;++j)a[u][j]=j^v?-a[u][j]:1;
    }
    printf("%.2f\n",-a[0][m]*100);
}

上一题