[线性规划 对偶原理 单纯形] BZOJ 3265 志愿者招募加强版

就是个裸题了


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

inline void read(ll &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=1005,M=10005;

int n,m;
ll Ans;
ll a[M][N];
int next[N];

inline void PIVOT(int l,int e)
{
	int last=-1;
	for (int i=0;i<=n;i++) if (i!=e) a[l][i]/=a[l][e];
	a[l][e]=1/a[l][e];
	memset(next,-1,sizeof(next));
	for (int i=0;i<=n;i++) if (a[l][i] && i!=e) next[i]=last,last=i;
	for (int i=0;i<=m;i++)
	{
		if (i==l || a[i][e]==0) continue;
		for (int j=last;j!=-1;j=next[j])
			a[i][j]-=a[i][e]*a[l][j];
		a[i][e]=-a[i][e]*a[l][e];
	}
}

inline ll Simplex(){
	int l,e,minimum;
	while (1){
		for (e=1;e<=n && a[0][e]<=0;e++);
		if (e==n+1) return -a[0][0];
		minimum=1<<30;
		for (int i=1;i<=m;i++)
			if (a[i][e]>0 && a[i][0]/a[i][e]<minimum)
				minimum=a[l=i][0]/a[i][e];
		PIVOT(l,e);
	}
}

int main()
{
	int k,s,t;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(m);
	for (int i=1;i<=n;i++) read(a[0][i]);
	for (int i=1;i<=m;i++)
	{
		read(k);
		while (k--){
			read(s); read(t);
			for (int j=s;j<=t;j++) 
				a[i][j]=1;
		}
		read(a[i][0]);
	}
	Ans=Simplex();
	printf("%lld\n",Ans);
	return 0;
}


你可能感兴趣的:([线性规划 对偶原理 单纯形] BZOJ 3265 志愿者招募加强版)