NC223592. CCheese,IfYouPlease
描述
输入描述
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); }