uoj#179. 线性规划
线性规划好珂怕啊……看了论文半天又看了Candy?巨佬的代码半天才勉强搞懂是个什么玩意儿……
//minamoto
#include<bits/stdc++.h>
#define R register
#define inf 1e18
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=25;const double eps=1e-8;
int n,m,op;double a[N][N],ans[N];int id[N<<1],q[N];
void pivot(int l,int e){
swap(id[n+l],id[e]);double t=a[l][e];a[l][e]=1;
fp(i,0,n)a[l][i]/=t;
fp(i,0,m)if(i!=l&&fabs(a[i][e])>eps){
t=a[i][e],a[i][e]=0;
fp(j,0,n)a[i][j]-=t*a[l][j];
}
}
bool init(){
while(true){
int e=0,l=0;
fp(i,1,m)if(a[i][0]<-eps&&(!l||rand()&1))l=i;if(!l)break;
fp(i,1,n)if(a[l][i]<-eps&&(!e||rand()&1))e=i;if(!e)return puts("Infeasible"),0;
pivot(l,e);
}return true;
}
bool simplex(){
while(true){
int l=0,e=0;double mn=inf;
fp(i,1,n)if(a[0][i]>eps){e=i;break;}if(!e)break;
fp(i,1,m)if(a[i][e]>eps&&a[i][0]/a[i][e]<mn)mn=a[i][0]/a[i][e],l=i;
if(!l)return puts("Unbounded"),0;pivot(l,e);
}return true;
}
int main(){
srand(20030719);
// freopen("testdata.in","r",stdin);
n=read(),m=read(),op=read();
fp(i,1,n)a[0][i]=read();
fp(i,1,m){
fp(j,1,n)a[i][j]=read();
a[i][0]=read();
}fp(i,1,n)id[i]=i;
if(init()&&simplex()){
printf("%.8lf\n",-a[0][0]);
if(op){
fp(i,1,m)ans[id[i+n]]=a[i][0];
fp(i,1,n)printf("%.8lf ",ans[i]);
}
}return 0;
}
深深地明白自己的弱小