UVA 10498 Happiness(线性规划-单纯形)

时间:2023-02-15 06:41:28

Description

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

题目大意:某人要买套餐给m个人吃,每个人吃的套餐必须都是一样的。套餐由n种食品组成,每种食品的单位价格是已知的。由于口味不同,每个人每得到一单位的某种食品获得的满足度可能不一样,这些也是已知的。每个人满足度是有上限的,上限已知。求在不超过任何人的满足度上限的条件下,此人最多能花多少钱。(某种食品可以买实数单位)

思路:线性规划的模板题。可以参考IOI国家集训队论文《浅谈信息学竞赛中的线性规划——简洁高效的单纯形法实现与应用》(上面的题目大意也是来自这里……)或者《算法导论》。

代码(32MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const double EPS = 1e-;
const int MAXN = ;
const int INF = 0x3fff3fff; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} double A[MAXN][MAXN], tA[MAXN][MAXN];
double b[MAXN], tb[MAXN], c[MAXN], tc[MAXN];
int N[MAXN], B[MAXN];
int n, m;
double v; bool init() {
N[] = B[] = v = ;
for(int i = ; i <= n; ++i) N[++N[]] = i;
for(int i = ; i <= m; ++i) B[++B[]] = n + i;
return true;
} void pivot(int l, int e) {
tb[e] = b[l] / A[l][e];
tA[e][l] = 1.0 / A[l][e];
for(int i = ; i <= N[]; ++i)
if(N[i] != e) tA[e][N[i]] = A[l][N[i]] / A[l][e];
for(int i = ; i <= B[]; ++i) {
tb[B[i]] = b[B[i]] - A[B[i]][e] * tb[e];
tA[B[i]][l] = -A[B[i]][e] * tA[e][l];
for(int j = ; j <= N[]; ++j)
if(N[j] != e) tA[B[i]][N[j]] = A[B[i]][N[j]] - tA[e][N[j]] * A[B[i]][e];
}
v += tb[e] * c[e];
tc[l] = -tA[e][l] * c[e];
for(int i = ; i <= N[]; ++i)
if(N[i] != e) tc[N[i]] = c[N[i]] - tA[e][N[i]] * c[e];
for(int i = ; i <= N[]; ++i) if(N[i] == e) N[i] = l;
for(int i = ; i <= B[]; ++i) if(B[i] == l) B[i] = e;
for(int i = ; i <= B[]; ++i) {
for(int j = ; j <= N[]; ++j)
A[B[i]][N[j]] = tA[B[i]][N[j]];
b[B[i]] = tb[B[i]];
}
for(int i = ; i <= N[]; ++i) c[N[i]] = tc[N[i]];
} bool simplex() {
while(true) {
int e = MAXN;
for(int i = ; i <= N[]; ++i)
if(c[N[i]] > EPS && N[i] < e) e = N[i];
if(e == MAXN) break;
double delta = -;
int l = MAXN;
for(int i = ; i <= B[]; ++i) {
if(sgn(A[B[i]][e]) > ) {
double tmp = b[B[i]] / A[B[i]][e];
if(delta == - || sgn(tmp - delta) < || (sgn(tmp - delta) == && B[i] < l)) {
delta = tmp;
l = B[i];
}
}
}
if(l == MAXN) return false;
pivot(l, e);
}
return true;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = ; i <= n; ++i) scanf("%lf", &c[i]);
for(int i = ; i <= m; ++i) {
for(int j = ; j <= n; ++j) scanf("%lf", &A[n + i][j]);
scanf("%lf", &b[n + i]);
}
init();
simplex();
printf("Nasa can spend %d taka.\n", (int)ceil(v * m));
}
}

研究发现那个用来保存新的矩阵的数组并不是必要的,于是删掉。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const double EPS = 1e-;
const int MAXN = ;
const int INF = 0x3fff3fff; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} double A[MAXN][MAXN];
double b[MAXN], c[MAXN];
int N[MAXN], B[MAXN];
int n, m;
double v; bool init() {
N[] = B[] = v = ;
for(int i = ; i <= n; ++i) N[++N[]] = i;
for(int i = ; i <= m; ++i) B[++B[]] = n + i;
return true;
} void pivot(int l, int e) {
b[e] = b[l] / A[l][e];
A[e][l] = 1.0 / A[l][e];
for(int i = ; i <= N[]; ++i) {
int &x = N[i];
if(x != e) A[e][x] = A[l][x] / A[l][e];
}
for(int i = ; i <= B[]; ++i) {
int &y = B[i];
b[y] -= A[y][e] * b[e];
A[y][l] = -A[y][e] * A[e][l];
for(int j = ; j <= N[]; ++j) {
int &x = N[j];
if(x != e) A[y][x] -= A[e][x] * A[y][e];
}
}
v += b[e] * c[e];
c[l] = -A[e][l] * c[e];
for(int i = ; i <= N[]; ++i) {
int &x = N[i];
if(x != e) c[x] -= A[e][x] * c[e];
}
for(int i = ; i <= N[]; ++i) if(N[i] == e) N[i] = l;
for(int i = ; i <= B[]; ++i) if(B[i] == l) B[i] = e;
} bool simplex() {
while(true) {
int e = MAXN;
for(int i = ; i <= N[]; ++i) {
int &x = N[i];
if(sgn(c[x]) > && x < e) e = x;
}
if(e == MAXN) break;
double delta = -;
int l = MAXN;
for(int i = ; i <= B[]; ++i) {
int &y = B[i];
if(sgn(A[y][e]) > ) {
double tmp = b[y] / A[y][e];
if(delta == - || sgn(tmp - delta) < || (sgn(tmp - delta) == && y < l)) {
delta = tmp;
l = y;
}
}
}
if(l == MAXN) return false;
pivot(l, e);
}
return true;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = ; i <= n; ++i) scanf("%lf", &c[i]);
for(int i = ; i <= m; ++i) {
for(int j = ; j <= n; ++j) scanf("%lf", &A[n + i][j]);
scanf("%lf", &b[n + i]);
}
init();
simplex();
printf("Nasa can spend %d taka.\n", (int)ceil(v * m));
}
}