单纯形&&线性规划

沦为了背板子...wyfcyx的ppt

#include<bits/stdc++.h>
using namespace std;
const int N = 30;
const double eps = 1e-8, inf = 1e18; 
int n, m, l, e, t;
double k;
double a[N][N], ans[N];
int id[N << 1];
void pivot(int l, int e)
{
    swap(id[n + l], id[e]);
    double r = a[l][e]; a[l][e] = 1;
    for(int i = 0; i <= n; ++i) a[l][i] /= r;
    for(int i = 0; i <= m; ++i) if(i != l)
    {
        double r = a[i][e]; a[i][e] = 0;
        for(int j = 0; j <= n; ++j) a[i][j] -= r * a[l][j];
    }
}
int main()
{
    scanf("%d%d%d", &n, &m, &t);
    for(int i = 1; i <= n; ++i) scanf("%lf", &a[0][i]), id[i] = i;
    for(int i = 1; i <= m; ++i)
    {
        for(int j = 1; j <= n; ++j) scanf("%lf", &a[i][j]);
        scanf("%lf", &a[i][0]);
    }
    while(true)
    {
        l = e = 0; k = -eps;
        for(int i = 1; i <= m; ++i) if(a[i][0] < k) { l = i; k = a[i][0]; }
        if(!l) break;
        for(int i = n; i; --i) if(a[l][i] < -eps)
        { e = i; break; }
        if(!e) { puts("Infeasible"); return 0; }
        pivot(l, e);
    }
    while(true)
    {
        l = e = 0;
        for(int i = 1; i <= n; ++i) if(a[0][i] > eps)
        { e = i; break; }
        if(!e) break;
        k = inf;
        for(int i = 1; i <= m; ++i) if(a[i][e] > eps && a[i][0] / a[i][e] < k)
        { k = a[i][0] / a[i][e]; l = i; }
        if(!l) { puts("Unbounded"); return 0; }
        pivot(l, e);
    }
    printf("%.10f\n", -a[0][0]);
    if(!t) return 0;
    for(int i = 1; i <= m; ++i) ans[id[i + n]] = a[i][0];
    for(int i = 1; i <= n; ++i) printf("%.10f ", ans[i]);
    return 0;
}
View Code

 

posted @ 2017-05-14 00:01  19992147  阅读(89)  评论(0编辑  收藏  举报