单纯形模板
Posted mychael
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单纯形模板相关的知识,希望对你有一定的参考价值。
uoj179
输入为线性规划标准形式
[max ; z = sumlimits_{j = 1}^{n}c_jx_j]
[
left{
egin{aligned}
sumlimits_{j = 1}^{n}a_{ij}x_j = b_j quad i in [1,m] x_j ge 0 quad j in [1,n]
end{aligned}
ight.
]
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 25,maxm = 100005;
const double eps = 1e-8,INF = 1e15;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,id[maxn << 1];
double a[maxn][maxn],ans[maxn];
void Pivot(int l,int e){
swap(id[n + l],id[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 (i != l && abs(a[i][e]) > eps){
t = a[i][e]; a[i][e] = 0;
for (int j = 0; j <= n; j++) a[i][j] -= a[l][j] * t;
}
}
bool init(){
while (true){
int e = 0,l = 0;
for (int i = 1; i <= m; i++) if (a[i][0] < -eps && (!l|| (rand() & 1))) l = i;
if (!l) break;
for (int j = 1; j <= n; j++) if (a[l][j] < -eps && (!e || (rand() & 1))) e = j;
if (!e){puts("Infeasible"); return false;}
Pivot(l,e);
}
return true;
}
bool 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;
if (!l){puts("Unbounded"); return false;}
Pivot(l,e);
}
return true;
}
int main(){
srand(time(NULL));
n = read(); m = read(); int t = read();
REP(i,n) a[0][i] = read();
REP(i,m){
REP(j,n) a[i][j] = read();
a[i][0] = read();
}
REP(i,n) id[i] = i;
if (init() && simplex()){
printf("%.8lf
",-a[0][0]);
if (t){
REP(i,m) ans[id[n + i]] = a[i][0];
REP(i,n) printf("%.8lf ",ans[i]);
}
}
return 0;
}
以上是关于单纯形模板的主要内容,如果未能解决你的问题,请参考以下文章