• 单纯形模板


    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. ]

    其中初始化操作当且仅当初始解({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("%.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;
    }
    
    
  • 相关阅读:
    人事面试13
    人事面试测试篇1
    人事面试16
    人事面试15
    人事面试测试篇3
    人事面试测试篇2
    人事面试14
    Oracle Compile 编译 无效对象
    Oracle 移动数据文件的操作方法
    Oracle 9i 从9.2.0.1升级到 9.2.0.6 步骤
  • 原文地址:https://www.cnblogs.com/Mychael/p/9247942.html
Copyright © 2020-2023  润新知