标签:原理 ref include bzoj www. long pre lib strong
题
http://www.lydsy.com/JudgeOnline/problem.php?id=3112
解
根据对偶原理转变线性规划形式(截图来自吴一凡的《线性规划与单纯形算法》)
然后单纯形求解
#include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <cmath> using namespace std; //n个变量 m条约束 //c目标 b约束 namespace Linear_Programming{ const double INF=1e17; const double EPS=1e-7; const int N=10004,M=1004; int m,n; double A[M][N],b[M],c[N],v; void Pivot(int l,int e) { int i,j; b[l]/=A[l][e]; for(i=1;i<=n;i++) if(i!=e) A[l][i]/=A[l][e]; A[l][e]=1/A[l][e]; for(i=1;i<=m;i++) if(i!=l&&fabs(A[i][e])>EPS) { b[i]-=A[i][e]*b[l]; for(j=1;j<=n;j++) if(j!=e) A[i][j]-=A[i][e]*A[l][j]; A[i][e]=-A[i][e]*A[l][e]; } v+=c[e]*b[l]; for(i=1;i<=n;i++) if(i!=e) c[i]-=c[e]*A[l][i]; c[e]=-c[e]*A[l][e]; } double Simplex() { int i,l,e; while(1) { for(i=1;i<=n;i++) if(c[i]>EPS) break; if((e=i)==n+1) return v; double temp=INF; for(i=1;i<=m;i++) if( A[i][e]>EPS && b[i]/A[i][e]<temp ) temp=b[i]/A[i][e],l=i; if(temp==INF) return INF; Pivot(l,e); } } } using namespace Linear_Programming; int main() { int i,j,xa,xb; double xc; scanf("%d%d",&m,&n); memset(A,0,sizeof(A)); for(i=1;i<=m;i++) scanf("%lf",&b[i]); for(i=1;i<=n;i++) { scanf("%d%d%lf",&xa,&xb,&xc); c[i]=xc; for(j=xa;j<=xb;j++) A[j][i]=1; } double ans=Simplex(); printf("%lld\n",(long long)ans); return 0; }
标签:原理 ref include bzoj www. long pre lib strong
原文地址:http://www.cnblogs.com/FxxL/p/7667419.html