码迷,mamicode.com
首页 > 其他好文 > 详细

道路修建 (网络流)

时间:2015-06-12 18:52:48      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

  1 const INF=2000000000;
  2 const maxn=4008;
  3 var r:array[0..maxn] of longint;
  4     eg:array[0..1000008] of record u,v,w,nt:longint; end;
  5     el:longint;
  6     lt:array[0..maxn] of longint;
  7     h,l:array[0..maxn] of longint;
  8     b:array[0..10008] of longint;
  9     i,j,k,n,s,t,m,x,y:longint;
 10 function op(i,j:longint):longint; inline;
 11 begin
 12     exit((i-1)*n+j);
 13 end;
 14 function calc(x,y:longint):longint; inline;
 15 begin
 16     exit(trunc(10*ln(233*(x-y)*(x-y)+1)));
 17 end;
 18 procedure adt(u,v,w:longint);
 19 begin
 20     inc(el);
 21     eg[el].u:=u;
 22     eg[el].v:=v;
 23     eg[el].w:=w;
 24     eg[el].nt:=lt[u];
 25     lt[u]:=el;
 26 end;
 27 procedure add(u,v,w:longint);
 28 begin
 29      //   writeln(u, ,v, ,w);
 30     adt(u,v,w); adt(v,u,0);
 31 end;
 32 procedure bfs;
 33 var i,l,r,x:longint;
 34 begin
 35     fillchar(h,sizeof(h),$7f);
 36     h[t]:=0;
 37     l:=1; r:=1; b[1]:=t;
 38     while l<=r do
 39     begin
 40         x:=b[l];
 41         i:=lt[x];
 42         while i<>0 do
 43         begin
 44             if (h[eg[i].v]>=n) and (eg[i xor 1].w>0) then
 45             begin
 46                 inc(r);
 47                 b[r]:=eg[i].v;
 48                 h[eg[i].v]:=h[x]+1;
 49             end;
 50             i:=eg[i].nt;
 51         end;
 52         inc(l);
 53     end;
 54 end;
 55 function min(a,b:longint):longint; inline;
 56 begin
 57     if a<b then exit(a) else exit(b);
 58 end;
 59 function dfs(u,inl:longint):longint;
 60 var i,v,outl:longint;
 61 begin
 62     if u=t then exit(inl);
 63     dfs:=0;
 64     i:=lt[u];
 65     while i<>0 do
 66     begin
 67         v:=eg[i].v;
 68         if (v>l[u]) and (eg[i].w>0) and (l[v]<n) and (h[u]=h[v]+1) then
 69         begin
 70             outl:=dfs(v,min(eg[i].w,inl));
 71             dec(inl,outl);
 72             inc(dfs,outl);
 73             dec(eg[i].w,outl);
 74             inc(eg[i xor 1].w,outl);
 75             if inl=0 then break;
 76             inc(l[u]);
 77         end;
 78         i:=eg[i].nt;
 79     end;
 80 end;
 81 function dinic:longint;
 82 var sum:longint;
 83 begin
 84     bfs;
 85     sum:=0;
 86     while h[s]<n do
 87     begin
 88         fillchar(l,sizeof(l),0);
 89         sum:=sum+dfs(s,INF);
 90         bfs;
 91         //writeln(sum);
 92     end;
 93     exit(sum);
 94 end;
 95 begin
 96     //assign(input,1.in);reset(input);
 97     el:=1;
 98     readln(n,m);
 99     for i:=1 to n do read(r[i]);
100     s:=0; t:=n*n+1;
101     for i:=1 to n do
102     begin
103         if i=1 then add(s,op(i,1),calc(0,r[i]))
104                else add(s,op(i,1),INF);
105         for j:=2 to n do
106             if i<>1 then
107                 add(op(i,j-1),op(i,j),calc(j-1,r[i]))
108             else
109                 add(op(i,j-1),op(i,j),INF);
110         if i=1 then add(op(i,n),t,INF) else add(op(i,n),t,calc(n,r[i]))
111     end;
112 
113     for i:=1 to m do
114     begin
115         readln(x,y);
116         for k:=2 to n do
117         begin
118             add(op(x,k),op(y,k-1),INF);
119             add(op(y,k),op(x,k-1),INF);
120         end;
121     end;
122     n:=n*n+1;
123     writeln(dinic);
124 end.

 

道路修建 (网络流)

标签:

原文地址:http://www.cnblogs.com/rpSebastian/p/4572236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!