【UVa10498】Happiness(单纯形)

传送门


板子题没有题解。


代码:

#include
#define ll long long
#define re register
#define cs const

cs int N=25;
cs double eps=1e-12,INF=1e17;
int n,m;
double a[N][N];
inline void pivot(int l,int e){
	double t=a[l][e];a[l][e]=1;
	for(int re j=0;j<=n;++j)a[l][j]/=t;
	for(int re i=0;i<=m;++i)if(l!=i&&fabs(a[i][e])>0){
		t=a[i][e],a[i][e]=0;
		for(int re j=0;j<=n;++j)a[i][j]-=t*a[l][j];
	}
}
inline void simplex(){
	while(true){
		int l=0,e=0;double mn=INF;
		for(int re j=1;j<=n;++j)if(a[0][j]>eps){e=j;break;}
		if(!e)break;
		for(int re 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);
	}
}

signed main(){
	while(~scanf("%d%d",&n,&m)){
		for(int re i=1;i<=n;++i)scanf("%lf",&a[0][i]);
		for(int re i=1;i<=m;++i){
			for(int re j=1;j<=n;++j)scanf("%lf",&a[i][j]);
			scanf("%lf",&a[i][0]);
		}
		a[0][0]=0;simplex();
		printf("Nasa can spend %lld taka.\n",(ll)floor(-a[0][0]*m+1.0-eps));
	}
	return 0;
}

你可能感兴趣的:(_____数学_____)