UVA10498 Happiness 【单纯形】

题目链接

UVA10498

题解

模板题

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair(a,b)
#define cp pair
using namespace std;
const int maxn = 25,maxm = 100005;
const double eps = 1e-12,INF = 1e15;
int n,m;
double a[maxn][maxn];
void Pivot(int l,int e){
    double t = a[l][e]; a[l][e] = 1;
    for (int j = 0; j <= n; j++) a[l][j] /= t;
    for (int i = 0; i <= m; i++) if (l != i && fabs(a[i][e]) > 0){
        t = a[i][e]; a[i][e] = 0;
        for (int j = 0; j <= n; j++) a[i][j] -= a[l][j] * t;
    }
}
void simplex(){
    while (true){
        int l = 0,e = 0; double mn = INF;
        for (int j = 1; j <= n; j++) if (a[0][j] > eps){e = j; break;}
        if (!e) break;
        for (int i = 1; i <= m; i++) if (a[i][e] > eps && a[i][0] / a[i][e] < mn)
            mn = a[i][0] / a[i][e],l = i;
        Pivot(l,e);
    }
}
int main(){
    while (~scanf("%d%d",&n,&m)){
        REP(j,n) scanf("%lf",&a[0][j]);
        REP(i,m){
            REP(j,n) scanf("%lf",&a[i][j]);
            scanf("%lf",&a[i][0]);
        }
        a[0][0] = 0;
        simplex();
        printf("Nasa can spend %lld taka.\n",(LL)floor((-a[0][0]) * m + 1 - eps));
    }
    return 0;
}

转载于:https://www.cnblogs.com/Mychael/p/9270036.html

你可能感兴趣的:(UVA10498 Happiness 【单纯形】)