【链接】http://acm.hdu.edu.cn/showproblem.php?pid=1533


【题意】


一个N*M地图上有相同数量的字符H和字符m,m代表一个 人,H代表一个房子。人到房子的花销是它们在图中的曼哈顿距离,问你让所有人回到房子所需要的最小费用(一个房子只能容纳一个人)。

【题解】


费用流;
建立一个超级源点,它和每个房子都有一条边相连,边的容量为1,费用为0;
建立一个超级汇点,他和每个人都有一条边相连,边的容量为1,费用为0;
每个房子和每个人都有一条边,容量为1,费用为它们的曼哈顿距离
这个图的最大流肯定是房子的个数.

则这个时候跑一下最小费用最大流就好.

【错的次数】


0

【反思】


第一次写费用流

【代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 100;
const int NN = 3e4;
const int INF = 0x3f3f3f3f; struct abc{
    int from,en,flow,nex,cost;
}; int n,m,totm,fir[2*N+50],dis[N*2+50],pre[2*N+50],mi[2*N+50],ans;
char s[N+10][N+10];
vector <pii> H,M;
abc bian[NN];
bool inq[2*N+50];
queue <int> dl; void add(int x,int y,int flow,int cost){
    bian[totm].nex = fir[x];
    fir[x] = totm;
    bian[totm].from = x;
    bian[totm].en = y;
    bian[totm].cost = cost;
    bian[totm].flow = flow;
    totm++;     bian[totm].nex = fir[y];
    fir[y] = totm;
    bian[totm].from = y;
    bian[totm].en = x;
    bian[totm].cost = -cost;
    bian[totm].flow = 0;
    totm++;
} bool spfa(int s,int t){
    ms(dis,INF),ms(inq,0),ms(mi,INF);
    dis[s] = 0,inq[s] = 1;
    dl.push(s);
    pre[t] = -1;
    while (!dl.empty()){
        int x = dl.front();
        inq[x] = false;
        dl.pop();
        for (int i = fir[x];i!=-1;i = bian[i].nex){
            int y = bian[i].en;
            if (bian[i].flow && dis[y] > dis[x] + bian[i].cost){
                dis[y] = dis[x] + bian[i].cost;
                mi[y] = min(bian[i].flow,mi[x]);
                pre[y] = i;
                if (!inq[y]){
                    inq[y] = true;
                    dl.push(y);
                }
            }
        }
    }
    if (pre[t]==-1) return false;
    int now = t;
    while (now != s){
        int temp = pre[now];
        bian[temp].flow -= mi[t];
        bian[temp^1].flow += mi[t];
        now = bian[temp].from;
        ans += mi[t]*bian[temp].cost;
    }
    return true;
} int main(){
    //Open();
    //Close();
    while (~ri(n)){
        ans = 0;
        ri(m);
        if (n == 0 && m == 0) break;
        rep1(i,0,n-1)
            rs(s[i]);
        H.clear(),M.clear();
        rep1(i,0,n-1)
            rep1(j,0,m-1)
                if (s[i][j]=='H')
                    H.pb(mp(i,j));
                else if (s[i][j]=='m')
                        M.pb(mp(i,j));
        totm = 0;
        ms(fir,255);
        int len1 = H.size(),len2 = M.size();
        rep1(i,0,len1-1) add(0,i+1,1,0);
        rep1(i,0,len2-1) add(i+1+len1,len1+len2+1,1,0);
        rep1(i,0,len1-1)
            rep1(j,0,len2-1)
                add(i+1,len1+j+1,1,abs(M[j].fi-H[i].fi)+abs(M[j].se-H[i].se));
        while (spfa(0,len1+len2+1));
        oi(ans);puts("");
    }
    return 0;
}

【hdu 1533】Going Home的更多相关文章

  1. 【HDU 1533】 Going Home (KM)

    Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, ...

  2. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  3. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  4. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  5. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  6. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  9. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

随机推荐

  1. Android严苛模式StrictMode使用详解

    StrictMode类是Android 2.3 (API 9)引入的一个工具类,可以用来帮助开发者发现代码中的一些不规范的问题,以达到提升应用响应能力的目的.举个例子来说,如果开发者在UI线程中进行了 ...

  2. BootstrapDialog模态框

    5最近是比较烦直接使用Bootstrap里面的模态框,满屏都是模态框代码,看得心烦.然后想起以前使用的BootstrapDialog.show()的方式,挺简单好用的.然后就拿出来分享一下. 1.下载 ...

  3. 参考《利用Python进行数据分析(第二版)》高清中文PDF+高清英文PDF+源代码

    第2版针对Python 3.6进行全面修订和更新,涵盖新版的pandas.NumPy.IPython和Jupyter,并增加大量实际案例,可以帮助高效解决一系列数据分析问题. 第2版中的主要更新了Py ...

  4. U-BOOT概述及源码分析(一)

    嵌入式Linux系统从软件角度通常可以分为以下4个层次: 引导加载程序 | Linux内核 | 文件系统 | 用户应用程序 嵌入式Linux系统中典型分区结构: 正常启动过程中,Bootloader首 ...

  5. CSUOJ 1526 Beam me out!

    Beam me out! King Remark, first of his name, is a benign ruler and every wrongdoer gets a second cha ...

  6. bzoj1084【SCOI2005】最大子矩阵

    1084: [SCOI2005]最大子矩阵 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1946  Solved: 970 [Submit][id ...

  7. 基于r-Kernel的LiteOS操作系统

    LiteOS是应用于资源受限的传感网络的一种基于线程的类UNIX操作系统.也就是说它跑在存储空间和RAM有限的超低电压微控制器上,这也是吸引我关注它的原因(在超低电压下系统更易出错).它採用r-ker ...

  8. NYOJ 203 三国志(Dijkstra+贪心)

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 <三国志>是一款非常经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.如今他把游戏简化一下 ...

  9. Vue的响应原理

    渲染render function之后就是 核心的响应式过程了 Object.defineProperty vue的核心之一就是Object.defineProperty 方法(IE9及其以上) Ob ...

  10. Kinect 开发 —— 骨骼数据与彩色影像和深度影像的对齐

    在显示彩色影像和深度影像时最好使用WriteableBitmap对象: 要想将骨骼数据影像和深度影像,或者彩色影像叠加到一起,首先要确定深度影像的分辨率和大小,为了方便,这里将深度影像数据和彩色影像数 ...