看了Nehzilrz神牛无比风骚的单纯形:POJ 1755 单纯形判定不等式是否成立,骤然对单纯形产生了浓厚的兴趣,遂苦苦钻研算导上的单纯性加上Nehzilrz神牛漂亮的代码,由此生成了我的第一道单纯形算法:
Nehzilrz神牛把目标函数,约束矩阵之类的全都塞到一个矩阵里了,整个代码行云流水,一气呵成!
这道题是poj1273,一道裸网络流,原usaco ditch。
code:
program dcx; const e=1e-6; var a:array[0..601,0..201] of real; next:array[0..200] of longint; n,m,x,y,z,i,j,k,p,tt:longint; t,min:real; begin while not seekeof do begin fillchar(a,sizeof(a),0); readln(m,n); for i:=1 to m do begin readln(x,y,z); a[x,i]:=1; if x<>1 then a[x+n,i]:=-1; if y<>n then a[y+n,i]:=1; if y<>n then a[y,i]:=-1; a[i+n+n,i]:=-1; a[i+n+n,0]:=z; end; while true do begin for i:=1 to m+1 do if a[1,i]>e then break; if i>m then break; min:=1e30;tt:=-1; for j:=2 to n+n+m do if (a[j,i]<-e)and(a[j,0]/-a[j,i]<min) then begin min:=a[j,0]/-a[j,i]; k:=j; end; t:=-1/a[k,i];a[k,i]:=-1; for j:=0 to m do a[k,j]:=a[k,j]*t; for j:=0 to m do if abs(a[k,j])>e then begin next[j]:=tt;tt:=j; end; for j:=1 to n+n+m do if j<>k then begin t:=a[j,i];a[j,i]:=0; p:=tt; while p<>-1 do begin a[j,p]:=a[j,p]+t*a[k,p]; p:=next[p]; end; end; end; writeln(a[1,0]:0:0); end; end.