单纯形法

代码为全幺模矩阵,故用整数。有贪心选取变量减少迭代次数。

暂时还不会初始化解,只能裸一裸。以后实现时一定要在草稿纸上计算清楚。

zjoi2013 defend 70分 code:

#include 
#include 
#include 
#include 
#include 
#include 

#define uns unsigned
#define int64 long long
#ifdef WIN32
#define fmt64 "%I64d"
#else
#define fmt64 "%lld"
#endif
#define oo 0x13131313
#define REP(i, n) for (i = 0; i < (n); ++i)

using namespace std;

int n, m, a[1005][10005];

void pivot(int u, int v)
{
	int i, j;
	REP(i, n + 1) if (i != v)
		a[u][i] /= a[u][v];
	a[u][v] = 1 / a[u][v];

	REP(j, m + 1) if (j != u) {
		for (i = 1; i <= n; ++i)
			if (i != v)
				a[j][i] -= a[j][v] * a[u][i];
		a[j][0] -= a[j][v] * a[u][0];
		a[j][v] *= -a[u][v];
	}
}

void simplex()
{
	int i, j, k, d, u, v, e;
	for (; ; ) {
		u = v = -1, e = -oo;
		for (i = 1; i <= n; ++i)
			if (a[0][i] > 0) {
				d = oo;
				for (j = 1; j <= m; ++j)
					if (a[j][i] > 0 && a[j][0] / a[j][i] < d)
						d = a[j][0] / a[j][i], k = j;
				if (d == oo) return;
				if (a[0][i] * a[k][0] / a[k][i] > e)
					e = a[0][i] * a[k][0] / a[k][i], u = k, v = i;
			}
		if (~u) pivot(u, v); else break;
	}
}

int main ()
{
	freopen ("defend.in", "r", stdin);
	freopen ("defend.out", "w", stdout);

	int i, j;
	scanf("%d%d", &m, &n);
	for (i = 1; i <= m; ++i)
		scanf("%d", a[i]);
	for (i = 1; i <= n; ++i) {
		int l, r;
		scanf("%d%d%d", &l, &r, a[0] + i);
		for (j = l; j <= r; ++j) a[j][i] = 1;
	}
	simplex();
	printf("%d\n", -a[0][0]);
}


你可能感兴趣的:(单纯形法)