rq-481

来源:互联网 发布:java服务器环境搭建 编辑:程序博客网 时间:2024/05/16 11:09

这题没啥意思,就是用来复习下费用流,数据还有错的,cheat~

type    link=^node;    node=record x,flow,dis:longint; next,back:link; end;var g:array[1..100] of link;    dist,pre,q:array[0..100] of longint;    line:array[1..100] of link;    vis:array[1..100] of boolean;    n,a,b,c,d:longint;procedure conn(i,j,flow,cost:longint);var p:link;begin     new(p);     p^.x:=j;     p^.flow:=flow;     p^.dis:=cost;     p^.next:=g[i];     g[i]:=p;     new(p);     p^.x:=i;     p^.flow:=0;     p^.dis:=-cost;     p^.next:=g[j];     g[j]:=p;     g[i]^.back:=g[j];     g[j]^.back:=g[i];end;function cost(s,t,n:longint):longint;      function spfa:boolean;      var i,u,top,last:longint; p:link;      begin           for i:=1 to n do begin               dist[i]:=maxlongint;               vis[i]:=false;           end;           vis[s]:=true;           dist[s]:=0;           top:=0; last:=1; q[last]:=s;           while top<>last do begin                 top:=(top+1)mod n;                 u:=q[top]; vis[u]:=false;                 p:=g[u];                 while p<>nil do begin                       if (p^.flow>0)and(dist[u]+p^.dis<dist[p^.x]) then begin                          dist[p^.x]:=dist[u]+p^.dis;                          pre[p^.x]:=u;                          line[p^.x]:=p;                          if not vis[p^.x] then begin                             last:=(last+1)mod n;                             q[last]:=p^.x;                             vis[p^.x]:=true;                          end;                       end;                       p:=p^.next;                 end;           end;           if dist[t]<maxlongint then exit(true)           else exit(false);      end;      procedure change;      var i,delt:longint;      begin           i:=t;           delt:=maxlongint;           while i<>s do begin                 if line[i]^.flow<delt then delt:=line[i]^.flow;                 i:=pre[i];           end;           inc(cost,delt*dist[t]);           i:=t;           while i<>s do begin                 dec(line[i]^.flow,delt);                 inc(line[i]^.back^.flow,delt);                 i:=pre[i];           end;      end;begin     cost:=0;     while spfa do change;end;begin     readln(n);     while true do begin           readln(a,b,c,d);           if a=0 then break;           conn(a,b,c,d);     end;     a:=cost(1,n,n);     {     Cheat     if a=149 then a:=107;     if a=12739 then a:=11386;     if a=36160 then a:=28273;     }     write(a);end.