BZOJ3265【单纯形】

/* I will wait for you*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define make make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair pii;

const int maxn = 10010;
const int maxm = 1010;
const int maxs = 26;
const double inf = 1e10;
const int P = 1000000007;
const double error = 1e-7;

inline ll read()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch <= 47 || ch >= 58)
		f = (ch == 45 ? -1 : 1), ch = getchar();
	while (ch >= 48 && ch <= 57)
		x = x * 10 + ch - 48, ch = getchar();
	return x * f;
}

double v, a[maxn][maxm], b[maxn], c[maxm];

int n, m, e, l;

void Pivot()
{
	b[l] /= a[l][e];
	for (int i = 1; i <= n; i++)
		if (i != e) a[l][i] /= a[l][e];
	a[l][e] = 1 / a[l][e];

	for (int i = 1; i <= m; i++)
		if (i != l && fabs(a[i][e]) > error) {
			b[i] -= a[i][e] * b[l];
			for (int j = 1; j <= n; j++)
				if (j != e) a[i][j] -= a[i][e] * a[l][j];
			a[i][e] = -a[i][e] * a[l][e];
		}

	v += c[e] * b[l];
	for (int i = 1; i <= n; i++)
		if (i != e) c[i] -= c[e] * a[l][i];
	c[e] = -c[e] * a[l][e];
}

double Simplex()
{
	while (true) {
		for (int &i = e = 1; i <= n; i++) 
			if (c[i] > error) break;
		if (e == n + 1) return v;

		double tmp = inf;
		for (int i = 1; i <= m; i++)
			if (a[i][e] > error && b[i] / a[i][e] < tmp)
				tmp = b[i] / a[i][e], l = i;
		Pivot();
	}
}

int main()
{
	n = read(), m = read();

	for (int i = 1; i <= n; i++) 
		scanf("%lf", &c[i]);

	for (int i = 1; i <= m; i++) {
		int size = read();
		for (int j = 1; j <= size; j++) {
			int u = read(), v = read();
			for (int k = u; k <= v; k++)
				a[i][k] += 1.0;
		}
		scanf("%lf", &b[i]);
	}
	
	double ans = Simplex();
	
	printf("%d\n", (int) (ans + 0.5));

	return 0;
}	

你可能感兴趣的:(计算几何)