1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include<bits/stdc++.h> using namespace std; const int M = 1<<20; const int maxn = 10000;
int m,n,n2; int x,y; double w[maxn]; int bv[maxn]; double C[maxn][maxn]; double check[maxn]; bool optimum_check(){ bool mark = true; double Max = -1,Min = maxn; for(int i = 0;i < n;i++){ int t = w[i]; for(int j = 0;j < m;j++) t -= w[bv[j]]*C[j][i]; check[i] = t; if(check[i] > 0){ mark = false; if(check[i] > Max){ Max = check[i]; y = i; } } } for(int i = 0;i < m;i++){ if(C[i][y] < 0 || abs(C[i][y] - 0) < 1e-10) continue; if(C[i][n]/C[i][y] <= Min){ Min = C[i][n]/C[i][y]; x = i; } } return mark; } int main(){ freopen("data.in","r",stdin); freopen("data.out","w",stdout); cin>>m>>n>>n2; for(int i = 0;i < n;i++) cin>>w[i]; for(int i = n;i < n + n2;i++) w[i] = -M; n += n2; for(int i = 0;i < m;i++){ for(int j = 0;j < n + 1;j++) cin>>C[i][j]; } int cnt = 0; memset(bv,-1,sizeof(bv)); for(int i = 0;i < n && cnt < m;i++){ if(C[cnt][i] != 1) continue; int mark = 1; for(int j = 0;j < m;j++){ if(j == cnt) continue; if(C[i][j] != 0) mark = 0; } if(mark) bv[cnt++] = i; } while(!optimum_check()){ double e = C[x][y]; for(int i = 0;i < n + 1;i++) C[x][i] /= e; for(int i = 0;i < m;i++){ if(i == x || !C[i][y]) continue; double t = C[i][y]/C[x][y]; for(int j = 0;j < n + 1;j++) C[i][j] -= C[x][j]*t; } bv[x] = y; } double optimum_value = 0,solu[n] = {0}; for(int i = 0;i < m;i++) solu[bv[i]] = C[i][n]; for(int i = 0;i < n;i++) optimum_value += solu[i]*w[i]; printf("The optimum value is %.2lf\n",optimum_value); printf("The solution is :\n"); for(int i = 0;i < n;i++){ printf("x%d = %.2lf\n",i+1,solu[i]); } return 0; }
|