每个格拆成两个点,出点连能到的点的入点,如果是箭头指向

方向费用就是0,要不就是1,源点连所有出点,所有入点连

汇点,然后费用流

/**************************************************************
    Problem:
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time: ms
    Memory: kb
****************************************************************/
 
//By BLADEVIL
var
    n, m                        :longint;
    map                         :array[..,..] of longint;
    pre, other, len, cost       :array[..] of longint;
    last                        :array[..] of longint;
    l                           :longint;
    num                         :array[..,..] of longint;
    go                          :array[..,..] of longint;
    source, sink                :longint;
    ans                         :longint;
    que, dis, father            :array[..] of longint;
    flag                        :array[..] of boolean;
     
function min(a,b:longint):longint;
begin
    if a>b then min:=b else min:=a;
end;
     
procedure connect(a,b,c,d:longint);
begin
    inc(l);
    pre[l]:=last[a];
    last[a]:=l;
    other[l]:=b;
    len[l]:=c;
    cost[l]:=d;
end;
     
     
procedure init;
var
    i, j, k                     :longint;
    c                           :char;
    curx, cury                  :longint;
     
begin
    readln(n,m);
    go[,]:=-; go[,]:=;
    go[,]:=; go[,]:=-;
    l:=;
    for i:= to n do
    begin
        for j:= to m do
        begin
            read(c);
            if c='U' then map[i,j]:= else
            if c='R' then map[i,j]:= else
            if c='D' then map[i,j]:= else
            if c='L' then map[i,j]:=;
        end;
        readln;
    end;
    for i:= to n do
        for j:= to m do num[i,j]:=((i-)*m+j);
    source:=*m*n+; sink:=source+;
    for i:= to n do
        for j:= to m do
            for k:= to do
            begin
                curx:=i+go[,k];
                cury:=j+go[,k];
                if curx= then curx:=n; if curx=n+ then curx:=;
                if cury= then cury:=m; if cury=m+ then cury:=;
                if map[i,j]=k then
                begin
                    connect(num[i,j]+n*m,num[curx,cury],,);
                    connect(num[curx,cury],num[i,j]+n*m,,);
                end else
                begin
                    connect(num[i,j]+n*m,num[curx,cury],,);
                    connect(num[curx,cury],num[i,j]+n*m,,-);
                end;
            end;
    for i:= to n do
        for j:= to m do
        begin
            connect(source,num[i,j]+n*m,,);
            connect(num[i,j]+n*m,source,,);
            connect(num[i,j],sink,,);
            connect(sink,num[i,j],,);
        end;
    {for i:=1 to n do
        for j:=1 to m do
        begin
            connect(num[i,j],num[i,j]+n*m,1,0);
            connect(num[i,j]+n*m,num[i,j],0,0);
        end;}
end;
 
function spfa:boolean;
var
    q, p, cur                   :longint;
    h, t                        :longint;
begin
    filldword(dis,sizeof(dis) div ,maxlongint div );
    h:=; t:=;
    que[]:=source; dis[source]:=;
    while h<>t do
    begin
        h:=h mod +;
        cur:=que[h];
        flag[cur]:=false;
        q:=last[cur];
        while q<> do
        begin
            p:=other[q];
            if len[q]> then
            begin
                if dis[p]>dis[cur]+cost[q] then
                begin
                    dis[p]:=dis[cur]+cost[q];
                    father[p]:=q;
                    if not flag[p] then
                    begin
                        t:=t mod +;
                        que[t]:=p;
                        flag[p]:=true;
                    end;
                end;
            end;
            q:=pre[q];
        end;
    end;
    if dis[sink]=maxlongint div then exit(false) else exit(true);
end;
 
procedure update;
var
    cur, low                    :longint;
begin
    cur:=sink;
    low:=maxlongint;
    while cur<>source do
    begin
        low:=min(low,len[father[cur]]);
        cur:=other[father[cur] xor ];
    end;
    cur:=sink;
    while cur<>source do
    begin
        dec(len[father[cur]],low);
        inc(len[father[cur] xor ],low);
        inc(ans,low*cost[father[cur]]);
        cur:=other[father[cur] xor ];
    end;
end;
 
procedure main;
begin
    while spfa do
        update;
    writeln(ans);
end;
 
begin
    init;
    main;
end.

bzoj 3171 费用流的更多相关文章

  1. bzoj 1449 费用流

    思路:先把没有进行的场次规定双方都为负,对于x胜y负 变为x + 1胜 y - 1 负所需要的代价为 2 * C[ i ] * x  - 2 * D[ i ] * y + C[ i ] + D[ i ...

  2. BZOJ 1061费用流

    思路: 我们可以列出几个不等式 用y0带进去变成等式 下-上 可以消好多东西 我们发现 等式左边的加起来=0 可以把每个方程看成一个点 正->负 连边 跑费用流即可 //By SiriusRen ...

  3. BZOJ 1283 费用流

    思路: 最大费用最大流 i->i+1 连边k 费用0 i->i+m (大于n的时候就连到汇) 连边1 费用a[i] //By SiriusRen #include <queue> ...

  4. bzoj 1070 费用流

    //可以网络流,但是要怎么分配每辆车让谁维修以及维修顺序呢.可以考虑每辆车维修时间对总结果的贡献,把每个修车人拆成n个点共n*m个点, //n辆车连向这n*m个点,流量1,费用k*修车时间,其中k(1 ...

  5. bzoj 2668 费用流

    我们可以把初始状态转化为目标状态这一约束转化为将黑子移动到目标状态所需要的最少步数. 除了初始点和目标点之外,剩下的点如果被经过那么就会被交换两次,所以我们将一个点拆成3个点,a,b,c,新建附加源点 ...

  6. bzoj 2245 费用流

    比较裸 源点连人,每个人连自己的工作,工作连汇,然后因为人的费用是 分度的,且是随工作数非降的,所以我们拆边,源点连到每个人s+1条边 容量是每段的件数,费用是愤怒 /**************** ...

  7. BZOJ 3280 费用流

    思路: 同BZOJ 1221 //By SiriusRen #include <queue> #include <cstdio> #include <cstring> ...

  8. BZOJ 4514 费用流

    思路: 懒得写了 http://blog.csdn.net/werkeytom_ftd/article/details/51277482 //By SiriusRen #include <que ...

  9. Bzoj 3171: [Tjoi2013]循环格 费用流

    3171: [Tjoi2013]循环格 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 741  Solved: 463[Submit][Status][ ...

随机推荐

  1. css中li、a、span行内强制不换行

    li.a.span行内强制不换行:white-space:nowrap; 没有之前的效果 加上white-space:nowrap;后

  2. Vue.js学习 Item11 – 组件与组件间的通信

    什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有 ...

  3. js-布尔值

    1.任何JavaScript的值都可以转换为布尔值 下面这些将会转换为false(假值): undefined null 0 -0 NaN "" //空字符串 所有其他值,包括所有 ...

  4. 【转】代码编辑器(一)-TSynCompletionProposal用法

    注意,本系列均转载自http://blog.163.com/zom1995@126/ 网上有人给我一个SynEdit这个东西,因为我很喜欢自己编个代码编辑器,但要是用Delphi直接弄的,就我现在这样 ...

  5. 高性能MySQL——第一章MySQL的架构与历史

    1.可以使用SHOW TABLE STATUS查询表的相关信息. 2.默认存储引擎是InnoDB,如果没有什么很特殊的要求,InnoDB引擎是我们最好的选择. 3.mysql的infobright引擎 ...

  6. VS活动解决方案平台

    测试环境:win7 x64 测试程序:WCF查询数据库后将数据集返回到Winform程序加载并显示 测试结果: 1.从感觉来说Exe在 x86目标平台生成,启动速度快. 2.内存消耗:x86的程序在超 ...

  7. SRF之数据字典

      框架提供数据字典的配置和显示的功能 字典以编码作为标识,用varchar(50)类型保存字典的编码.   字典的用法 1.在代码里边需要查询字典信息的 可用 Components.DataDict ...

  8. Python学习教程(learning Python)--1.1Python程序设计流程

    Python程序设计与其他高级语言程序设计流程基本一致     step1 程序设计     step2 编写Python代码     setp3 Python语句语法纠错     step4 测试程 ...

  9. 【Inno Setup】 Inno Setup 64位安装程序默认安装路径

    在脚本中加入: ArchitecturesInstallIn64BitMode=x64 ArchitecturesAllowed=x64

  10. Linux内核学习笔记——内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...