# UVA 10498 Happiness（线性规划-单纯形）

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.

 1 #include <cstdio>
2 #include <iostream>
3 #include <algorithm>
4 #include <cstring>
5 #include <cmath>
6 using namespace std;
7
8 const double EPS = 1e-10;
9 const int MAXN = 55;
10 const int INF = 0x3fff3fff;
11
12 inline int sgn(double x) {
13     return (x > EPS) - (x < -EPS);
14 }
15
16 double A[MAXN][MAXN], tA[MAXN][MAXN];
17 double b[MAXN], tb[MAXN], c[MAXN], tc[MAXN];
18 int N[MAXN], B[MAXN];
19 int n, m;
20 double v;
21
22 bool init() {
23     N[0] = B[0] = v = 0;
24     for(int i = 1; i <= n; ++i) N[++N[0]] = i;
25     for(int i = 1; i <= m; ++i) B[++B[0]] = n + i;
26     return true;
27 }
28
29 void pivot(int l, int e) {
30     tb[e] = b[l] / A[l][e];
31     tA[e][l] = 1.0 / A[l][e];
32     for(int i = 1; i <= N[0]; ++i)
33         if(N[i] != e) tA[e][N[i]] = A[l][N[i]] / A[l][e];
34     for(int i = 1; i <= B[0]; ++i) {
35         tb[B[i]] = b[B[i]] - A[B[i]][e] * tb[e];
36         tA[B[i]][l] = -A[B[i]][e] * tA[e][l];
37         for(int j = 1; j <= N[0]; ++j)
38             if(N[j] != e) tA[B[i]][N[j]] = A[B[i]][N[j]] - tA[e][N[j]] * A[B[i]][e];
39     }
40     v += tb[e] * c[e];
41     tc[l] = -tA[e][l] * c[e];
42     for(int i = 1; i <= N[0]; ++i)
43         if(N[i] != e) tc[N[i]] = c[N[i]] - tA[e][N[i]] * c[e];
44     for(int i = 1; i <= N[0]; ++i) if(N[i] == e) N[i] = l;
45     for(int i = 1; i <= B[0]; ++i) if(B[i] == l) B[i] = e;
46     for(int i = 1; i <= B[0]; ++i) {
47         for(int j = 1; j <= N[0]; ++j)
48             A[B[i]][N[j]] = tA[B[i]][N[j]];
49         b[B[i]] = tb[B[i]];
50     }
51     for(int i = 1; i <= N[0]; ++i) c[N[i]] = tc[N[i]];
52 }
53
54 bool simplex() {
55     while(true) {
56         int e = MAXN;
57         for(int i = 1; i <= N[0]; ++i)
58             if(c[N[i]] > EPS && N[i] < e) e = N[i];
59         if(e == MAXN) break;
60         double delta = -1;
61         int l = MAXN;
62         for(int i = 1; i <= B[0]; ++i) {
63             if(sgn(A[B[i]][e]) > 0) {
64                 double tmp = b[B[i]] / A[B[i]][e];
65                 if(delta == -1 || sgn(tmp - delta) < 0 || (sgn(tmp - delta) == 0 && B[i] < l)) {
66                     delta = tmp;
67                     l = B[i];
68                 }
69             }
70         }
71         if(l == MAXN) return false;
72         pivot(l, e);
73     }
74     return true;
75 }
76
77 int main() {
78     while(scanf("%d%d", &n, &m) != EOF) {
79         for(int i = 1; i <= n; ++i) scanf("%lf", &c[i]);
80         for(int i = 1; i <= m; ++i) {
81             for(int j = 1; j <= n; ++j) scanf("%lf", &A[n + i][j]);
82             scanf("%lf", &b[n + i]);
83         }
84         init();
85         simplex();
86         printf("Nasa can spend %d taka.\n", (int)ceil(v * m));
87     }
88 }
View Code

 1 #include <cstdio>
2 #include <iostream>
3 #include <algorithm>
4 #include <cstring>
5 #include <cmath>
6 using namespace std;
7
8 const double EPS = 1e-10;
9 const int MAXN = 55;
10 const int INF = 0x3fff3fff;
11
12 inline int sgn(double x) {
13     return (x > EPS) - (x < -EPS);
14 }
15
16 double A[MAXN][MAXN];
17 double b[MAXN], c[MAXN];
18 int N[MAXN], B[MAXN];
19 int n, m;
20 double v;
21
22 bool init() {
23     N[0] = B[0] = v = 0;
24     for(int i = 1; i <= n; ++i) N[++N[0]] = i;
25     for(int i = 1; i <= m; ++i) B[++B[0]] = n + i;
26     return true;
27 }
28
29 void pivot(int l, int e) {
30     b[e] = b[l] / A[l][e];
31     A[e][l] = 1.0 / A[l][e];
32     for(int i = 1; i <= N[0]; ++i) {
33         int &x = N[i];
34         if(x != e) A[e][x] = A[l][x] / A[l][e];
35     }
36     for(int i = 1; i <= B[0]; ++i) {
37         int &y = B[i];
38         b[y] -= A[y][e] * b[e];
39         A[y][l] = -A[y][e] * A[e][l];
40         for(int j = 1; j <= N[0]; ++j) {
41             int &x = N[j];
42             if(x != e) A[y][x] -= A[e][x] * A[y][e];
43         }
44     }
45     v += b[e] * c[e];
46     c[l] = -A[e][l] * c[e];
47     for(int i = 1; i <= N[0]; ++i) {
48         int &x = N[i];
49         if(x != e) c[x] -= A[e][x] * c[e];
50     }
51     for(int i = 1; i <= N[0]; ++i) if(N[i] == e) N[i] = l;
52     for(int i = 1; i <= B[0]; ++i) if(B[i] == l) B[i] = e;
53 }
54
55 bool simplex() {
56     while(true) {
57         int e = MAXN;
58         for(int i = 1; i <= N[0]; ++i) {
59             int &x = N[i];
60             if(sgn(c[x]) > 0 && x < e) e = x;
61         }
62         if(e == MAXN) break;
63         double delta = -1;
64         int l = MAXN;
65         for(int i = 1; i <= B[0]; ++i) {
66             int &y = B[i];
67             if(sgn(A[y][e]) > 0) {
68                 double tmp = b[y] / A[y][e];
69                 if(delta == -1 || sgn(tmp - delta) < 0 || (sgn(tmp - delta) == 0 && y < l)) {
70                     delta = tmp;
71                     l = y;
72                 }
73             }
74         }
75         if(l == MAXN) return false;
76         pivot(l, e);
77     }
78     return true;
79 }
80
81 int main() {
82     while(scanf("%d%d", &n, &m) != EOF) {
83         for(int i = 1; i <= n; ++i) scanf("%lf", &c[i]);
84         for(int i = 1; i <= m; ++i) {
85             for(int j = 1; j <= n; ++j) scanf("%lf", &A[n + i][j]);
86             scanf("%lf", &b[n + i]);
87         }
88         init();
89         simplex();
90         printf("Nasa can spend %d taka.\n", (int)ceil(v * m));
91     }
92 }
View Code

posted @ 2014-03-26 00:30 Oyking 阅读(...) 评论(...) 编辑 收藏