zoukankan      html  css  js  c++  java
  • bzoj2424

    比较简单的费用流,一目了然

      1 const inf=10000007;
      2 type node=record
      3        next,point,flow,cost:longint;
      4      end;
      5 
      6 var edge:array[0..200010] of node;
      7     q:array[0..200010] of longint;
      8     p,d,pre,cur,u,w:array[0..200] of longint;
      9     v:array[0..200] of boolean;
     10     s,i,len,n,m,t:longint;
     11 
     12 function min(a,b:longint):longint;
     13   begin
     14     if a>b then exit(b) else exit(a);
     15   end;
     16 
     17 procedure add(x,y,f,c:longint);
     18   begin
     19     inc(len);
     20     edge[len].point:=y;
     21     edge[len].flow:=f;
     22     edge[len].cost:=c;
     23     edge[len].next:=p[x];
     24     p[x]:=len;
     25   end;
     26 
     27 function spfa:boolean;
     28   var y,i,f,r,x:longint;
     29   begin
     30     d[0]:=0;
     31     for i:=1 to t do
     32       d[i]:=inf;
     33     f:=1;
     34     r:=1;
     35     q[1]:=0;
     36     fillchar(v,sizeof(v),false);
     37     v[0]:=true;
     38     while f<=r do
     39     begin
     40       x:=q[f];
     41       v[x]:=false;
     42       i:=p[x];
     43       while i<>-1 do
     44       begin
     45         y:=edge[i].point;
     46         if edge[i].flow>0 then
     47           if d[y]>d[x]+edge[i].cost then
     48           begin
     49             d[y]:=d[x]+edge[i].cost;
     50             pre[y]:=x;
     51             cur[y]:=i;
     52             if not v[y] then
     53             begin
     54               inc(r);
     55               q[r]:=y;
     56               v[y]:=true;
     57             end;
     58           end;
     59         i:=edge[i].next;
     60       end;
     61       inc(f);
     62     end;
     63     if d[t]=inf then exit(false) else exit(true);
     64   end;
     65 
     66 function mincost:longint;
     67   var neck,i,j:longint;
     68   begin
     69     mincost:=0;
     70     while spfa do
     71     begin
     72       neck:=inf;
     73       i:=t;
     74       while i<>0 do
     75       begin
     76         j:=cur[i];
     77         neck:=min(neck,edge[j].flow);
     78         i:=pre[i];
     79       end;
     80       i:=t;
     81       while i<>0 do
     82       begin
     83         j:=cur[i];
     84         dec(edge[j].flow,neck);
     85         inc(edge[j xor 1].flow,neck);
     86         i:=pre[i];
     87       end;
     88       mincost:=mincost+d[t]*neck;
     89     end;
     90   end;
     91 
     92 begin
     93   len:=-1;
     94   fillchar(p,sizeof(p),255);
     95   readln(n,m,s);
     96   t:=n+1;
     97   for i:=1 to n do
     98     read(u[i]);
     99   for i:=1 to n do
    100   begin
    101     read(w[i]);
    102     add(0,i,inf,w[i]);
    103     add(i,0,0,-w[i]);
    104     add(i,t,u[i],0);
    105     add(t,i,0,0);
    106     if i<>n then
    107     begin
    108       add(i,i+1,s,m);
    109       add(i+1,i,0,-m);
    110     end;
    111   end;
    112   writeln(mincost);
    113 end.
    View Code
  • 相关阅读:
    【Leetcode】Insertion Sort List JAVA实现
    【Leetcode】Sort List JAVA实现
    Maximum Product Subarray JAVA实现
    hadoop2.0中无法启动datanode的问题
    合并排序
    setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式
    postgresql 服务器端编程之hello word
    mac osx get postgresql path
    mac osx install mysql
    django 基于proxy实现用户权限管理
  • 原文地址:https://www.cnblogs.com/phile/p/4473072.html
Copyright © 2011-2022 走看看