我是靠谱客的博主 聪明翅膀,最近开发中收集的这篇文章主要介绍单纯形模板,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

uoj179
输入后转化为线性规划标准形式
[max ; z = sumlimits_{j = 1}^{n}c_jx_j]
[ left{ begin{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} right. ]
其中初始化操作当且仅当初始解({0,0,dots,0})不合法时调用

#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("%.8lfn",-a[0][0]);
if (t){
REP(i,m) ans[id[n + i]] = a[i][0];
REP(i,n) printf("%.8lf ",ans[i]);
}
}
return 0;
}

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

最后

以上就是聪明翅膀为你收集整理的单纯形模板的全部内容,希望文章能够帮你解决单纯形模板所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部