其他分享
首页 > 其他分享> > LP

LP

作者:互联网

#include <bits/stdc++.h>
#define fre(x) freopen( #x ".in", "r", stdin ), freopen( #x ".out", "w", stdout )
using namespace std; typedef long long ll; typedef unsigned long long ull; typedef __float128 db;
const int N = 40 + 10; const db eps = 1e-17, INF = 1e18;

mt19937 mt( (ull)(new char) );
int Rand ( int l, int r ) { uniform_int_distribution<>res(l,r); return res(mt); }

int n, m, t;

db fabs ( db a ) { return a < 0 ? -a : a; }

int Read ()
{
	int x = 0, u = 0; char c = getchar();
	for( ; !isdigit(c); c = getchar() ) if( c == '-' ) u = 1;
	for( ; isdigit(c); c = getchar() ) x = x * 10 + c - '0';
	return u ? -x : x;
}

struct LP
{
	int pos[N]; db arr[N][N], res[N];

	void Pivot ( int e, int l )
	{
		swap( pos[e], pos[n+l] ); // x_e 与 x_n+l 交换
		db lamda = -1 / arr[l][e]; // 主行归-1
		for( int i = 0; i <= n; ++i) if( i != e ) arr[l][i] *= lamda; arr[l][e] = -1 * lamda; // 其他直接乘,x_n+l原来是-1
		for( int i = 0; i <= m; ++i) if( i != l && fabs(arr[i][e]) > eps )
		{	db lamda = arr[i][e]; for( int j = 0; j <= n; ++j) if( j != e ) arr[i][j] += lamda * arr[l][j]; arr[i][e] = lamda * arr[l][e]; } // 等式的性质,e是n+l
	}

	bool Init ()
	{
		while( true )
		{
			int e = 0, l = 1;
			for( int i = 2; i <= m; ++i) if( !l || arr[l][0] > arr[i][0] ) l = i; if( arr[l][0] >= -eps ) return true; // 最小b的替出变量
			for( int i = 1; i <= n; ++i) if( arr[l][i] > eps && ( !e || pos[i] > pos[e] ) ) e = i; if( !e ) return false; // 寻找替入变量, bland规则
			Pivot( e, l );
		}
	}

	bool Simplex ()
	{
		while( true )
		{
			int e = 1, l = 0;
			for( int i = 2; i <= n; ++i) if( arr[0][e] > arr[0][i] ) e = i; if( arr[0][e] >= -eps ) return true; // 寻找最大c的替入变量
			db mini = 1e18;
			for( int i = 1; i <= m; ++i) if( arr[i][e] < -eps )
			{	db lamda = -arr[i][0] / arr[i][e]; if( !l || mini > lamda || ( fabs(mini-lamda) < eps && pos[i] > pos[l] ) ) mini = lamda, l = i; } // 找替出变量,bland
			if( !l ) return false;
			Pivot( e, l);
		}
	}
}LP;

void Solve ()
{
	n = Read(), m = Read(), t = Read();
	int x;
	for( int i = 1; i <= n; ++i) x = Read(), LP.arr[0][i] = -x;
	for( int i = 1; i <= m; ++i) { for( int j = 1; j <= n; ++j) x = Read(), LP.arr[i][j] = -x; x = Read(), LP.arr[i][0] = x; }
	for( int i = 1; i <= m+n; ++i) LP.pos[i] = i;
	if( !LP.Init() ) return cout << "Infeasible\n", void(); if( !LP.Simplex() ) return cout << "Unbounded\n", void();
	cout << fixed << setprecision(9) << (double)-LP.arr[0][0] << "\n";
	if( !t ) return;
	for( int i = 1; i <= m; ++i) LP.res[ LP.pos[n+i] ] = LP.arr[i][0];
	for( int i = 1; i <= n; ++i) cout << fixed << setprecision(9) << (double)LP.res[i] << " "; cout << "\n";
}

int main ()
{
	//fre(x);
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); Solve(); return 0;
}

标签:arr,return,int,db,pos,eps,LP
来源: https://www.cnblogs.com/LYinMX/p/16271762.html