BZOJ3112 [Zjoi2013]防守战线 【单纯形】

题目链接

BZOJ3112

题解

同志愿者招募
费用流神题
单纯形裸题
\(BZOJ\)可过
洛谷被卡。。

#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 = 1005,maxm = 10005;
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;
double a[maxm][maxn];
void Pivot(int l,int 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 && fabs(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;
	}
}
void init(){
	while (true){
		int l = 0,e = 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;
		Pivot(l,e);
	}
}
void 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;
		Pivot(l,e);
	}
}
int main(){
	srand(time(NULL));
	n = read(); m = read(); int L,R;
	REP(j,n) a[0][j] = -read();
	REP(i,m){
		L = read(); R = read();
		for (int j = L; j <= R; j++)
			a[i][j] = -1;
		a[i][0] = -read();
	}
	init(); simplex();
	printf("%lld\n",(LL)(a[0][0] + 0.5));
	return 0;
}

posted @ 2018-07-01 09:30  Mychael  阅读(234)  评论(0编辑  收藏  举报