BZOJ1061 [Noi2008]志愿者招募 【单纯形】

题目链接

BZOJ1061

题解

今天终于用正宗的线性规划\(A\)了这道题
题目可以看做有\(N\)个限制和\(M\)个变量
变量\(x_i\)表示第\(i\)种志愿者的人数,对于第\(i\)种志愿者所能触及的那些天,\(x_i\)的系数都为\(1\),其余为\(0\)
也就是

\[min \; z = \sum\limits_{i = 1}^{M} C_ix_i \\ \left\{ \begin{aligned} \sum\limits_{i = 1}^{M} [S_i \le j \le T_i]x_i \ge A_i \qquad j \in [1,N]\\ x_i \ge 0 \qquad i \in [1,M] \end{aligned} \right. \]

转化为标准型线性规划,使用单纯形算法求解即可
诶?解保证是整数吗?

似乎相对于费用流,空间大且跑得慢,,,

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#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 N = 1005,M = 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,id[M << 1];
double a[N][M];
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 && 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 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;
		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)); int S,T,C;
	m = read(); n = read();
	REP(i,m) a[i][0] = -read();
	REP(j,n){
		S = read(); T = read(); C = read();
		for (int i = S; i <= T; i++)
			a[i][j] = -1;
		a[0][j] = -C;
	}
	REP(i,n) id[i] = i;
	init(); simplex();
	printf("%d",(int)(a[0][0] + 0.5));
	return 0;
}

posted @ 2018-06-30 19:00  Mychael  阅读(268)  评论(0编辑  收藏  举报