一开始我觉得这不是一个弱弱的费用流吗?

每个豆豆拆点,入点出点随便连连

由于肯定是DAG图,边权为正的最大费用肯定能增广出来

于是我们只要跑总流量为2的最大费用最大流不就行了吗

但是

这样会TLE,因为会出现稠密图,spfa跑稠密图太慢

既然这样,能不能换一个找增广路的方法呢?

最大路径更快的方法就是DAG图的拓扑排序+dp

但好像无法处理反向弧导致带来的环啊

那既然这样,反正总流量很小,

我们可以用DAG解决最大路径特有的dp找第一条增广路(因为这时候是肯定的DAG),跑出一个较大费用

然后再用spfa增广呢?这样就大大减小了时间复杂度

这就是AC方法

注意这道题空间卡的较紧,能开integer不要开longint

实现起来要小心

 const mo=;
type node=record
       next:longint;
       point,flow,cost:integer;
     end; var edge:array[..] of node;
    v:array[..] of boolean;
    d,rd,pre,pref:array[..] of integer;
    q:array[..] of integer;
    p,x,y,cur:array[..] of longint;
    len:longint;
    max,s,t,i,j,n,m,be:integer; procedure add(x,y,f,c:integer);
  begin
    edge[len].point:=y;
    edge[len].flow:=f;
    edge[len].cost:=c;
    edge[len].next:=p[x];
    p[x]:=len;
  end; procedure preflow;
  var x,y,f,r,i,j:longint;
  begin
    r:=;
    f:=;
    q[]:=s;
    while f<=r do  //拓扑排序
    begin
      x:=q[f];
      i:=p[x];
      while i<>- do
      begin
        y:=edge[i].point;
        dec(rd[y]);
        if (rd[y]=) then
        begin
          inc(r);
          q[r]:=y;
        end;
        i:=edge[i].next;
      end;
      inc(f);
    end;
    fillchar(d,sizeof(d),);
    d[s]:=;
    for j:= to r do //DAG图根据拓扑的顺序dp解决最大路径
    begin
      x:=q[j];
      i:=p[x];
      while i<>- do
      begin
        y:=edge[i].point;
        if d[y]<d[x]+ then
        begin
          d[y]:=d[x]+;
          pre[y]:=x;
          cur[y]:=i;
          if d[y]>d[max] then max:=y;
        end;
        i:=edge[i].next;
      end;
    end;
  end; procedure change;   //建立预处理后的网络继续增广
  var i,j,e,w:longint;
  begin
    for i:= to n do
      pref[i]:=;
    i:=max;
    while i<>s do   //相当于找到第一条增广路然后弄一弄反向弧什么的
    begin
      pref[i]:=;
      j:=cur[i];
      dec(edge[j].flow);
      if pre[i]=s then w:=i;
      i:=pre[i];
    end;
    len:=-;
    for i:= to n do
      for j:= to n do
        if (i<>j) then
          if (x[i]<=x[j]) and (y[i]<=y[j]) then
          begin
            inc(len);
            edge[len].point:=j+n;
            inc(len);
            add(j+n,i,-edge[len-].flow,)  //添加原来没加的反向弧
          end;     p[s]:=-;
    for i:= to n do   //下面很多处理的细节,不赘述,繁杂但不难想到
    begin
      if i=w then e:= else e:=;
      inc(len);
      add(s,i+n,e,);
      inc(len);
      add(i+n,s,-e,);
    end;
// 表示超级源点,s表示起点,t表示超级汇点
    t:=*n+;
    inc(len);
    add(,s,,);
    inc(len);
    add(s,,,);
    for i:= to n do   
    begin
      inc(len);
      add(i+n,i,pref[i],);
      inc(len);
      add(i,i+n,-pref[i],-);
      if max=i then e:=   
      else e:=;
      inc(len);
      add(i,t,e,);
      inc(len);
      add(t,i,-e,);
    end;
  end; function spfa:boolean;
  var f,r,x,y,i,j,head,tail:longint;
  begin
    fillchar(v,sizeof(v),false);
    for i:= to t do
      d[i]:=-mo;
    d[]:=;
    q[]:=;
    f:=;
    r:=;
    head:=;
    tail:=;
    while head<=tail do  //缩空间的写法,好久没这么写了
    begin
      x:=q[f];
      v[x]:=false;
      i:=p[x];
      while i<>- do
      begin
        y:=edge[i].point;
        if edge[i].flow> then
        begin
          if d[x]+edge[i].cost>d[y] then
          begin
            d[y]:=edge[i].cost+d[x];
            pre[y]:=x;
            cur[y]:=i;
            if not v[y] then
            begin
              v[y]:=true;
              r:=(r+) mod mo;
              inc(tail);
              q[r]:=y;
            end;
          end;
        end;
        i:=edge[i].next;
      end;
      f:=(f+) mod mo;
      inc(head);
    end;
    if d[t]=-mo then exit(false) else exit(true);
  end; function mincost:longint;  //最大费用最大流
  var i,j:longint;
  begin
    be:=d[max];
    mincost:=be;   //预处理出来的费用
    while spfa do
    begin
      mincost:=mincost+d[t];
      i:=t;
      while i<> do
      begin
        j:=cur[i];
        dec(edge[j].flow);
        inc(edge[j xor ].flow);
        i:=pre[i];
      end;
    end;
  end; begin
  len:=-;
  fillchar(p,sizeof(p),);
  readln(n);
  for i:= to n do
    readln(x[i],y[i]);
  for i:= to n do
    for j:= to n do
      if (i<>j) then
        if (x[i]<=x[j]) and (y[i]<=y[j]) then
        begin
          len:=len+;
          add(i,j,,);  //个人感觉这样建之后添加边更容易一点
          inc(rd[j]);
        end;   s:=*n+;
  for i:= to n do
    if rd[i]= then
    begin
      len:=len+;
      add(s,i,,);
      inc(rd[i]); 
    end;   preflow;
  change;
  writeln(mincost);
end.

bzoj1930的更多相关文章

  1. 【BZOJ1930】[Shoi2003]pacman 吃豆豆 最大费用最大流

    [BZOJ1930][Shoi2003]pacman 吃豆豆 Description 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会 ...

  2. BZOJ1930 [Shoi2003]pacman 吃豆豆

     dp,首先建出图,f[i][j]表示a吃到了i点,b吃到了j点的最大值,转移的时候转移拓扑序小的那一维,如果i拓扑序小于j,那么转移到f[k][j],否则转移到f[i][k],建出的图边数也要优化, ...

  3. 【BZOJ1930】【SHOI2003】吃豆豆

    初见杀…… 原题: 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会吃掉它.PACMAN行走的路线很奇怪,只能向右走或者向上走,他们行 ...

  4. [转载]hzwer的bzoj题单

    counter: 664BZOJ1601 BZOJ1003 BZOJ1002 BZOJ1192 BZOJ1303 BZOJ1270 BZOJ3039 BZOJ1191 BZOJ1059 BZOJ120 ...

  5. BZOJ刷题列表【转载于hzwer】

    沿着黄学长的步伐~~ 红色为已刷,黑色为未刷,看我多久能搞完吧... Update on 7.26 :之前咕了好久...(足见博主的flag是多么emmm......)这几天开始会抽时间刷的,每天几道 ...

随机推荐

  1. Ajax的概述与实现过程

    一.ajax概述 1.Ajax是Asynchronous([ə'sɪŋkrənəs) JavaScript XML的简写,不是一门新技术,而是对现有技术的综合利用.这一技术能够向服务器请求额外数据而无 ...

  2. HTML5之图像处理

    --- 内嵌图像 - drawImage可以绘制图像context.drawImage(image,dx,dy)context.drawImage(image,dx,dy,dw,dh)context. ...

  3. Django部署问题

    1.Debug=True页面正常显示. 2.Debug=False,页面500错误. 3.解决500,配置setting.py,令ALLOWED_HOSTS = ['*'],可解决访问问题,但静态文件 ...

  4. 【HeadFirst设计模式】13.与设计模式相处

    模式: 是在某情境下,针对某问题的某种解决方案. 要点: 让设计模式自然而然地出现在你的设计中,而不是为了使用而使用. 设计模式并非僵化的教条,你可以依据自己的需要采用或者进行调整. 总是使用最简单的 ...

  5. Linux Vi的使用

    1.vi使用三模式:一般模式,插入模式,命令模式 保存和退出vi: 命令模式下 :w 保存 :w 新文件 保存到新文件 类似另存为,新文件存在,报错 :w! 新文件 保存到新文件,新文件存在,覆盖 : ...

  6. Sublime Text3注册码

    这是一个注册码-– BEGIN LICENSE -– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 ...

  7. centos系统python升级2.7.3

    首先下载源tar包 可利用linux自带下载工具wget下载,如下所示: wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz 下载 ...

  8. 【加密】RSA加密之算法

    RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的. RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥, ...

  9. hdu 4746 Mophues 莫比乌斯反演+前缀和优化

    Mophues 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<=n, 1<=b<=m) 有Q组数据:(n, m, ...

  10. 开发软件设计模型 visual studio UML

    http://www.ibm.com/developerworks/cn/rational/rationaledge/content/feb05/bell/ http://msdn.microsoft ...