【链接】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;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626088.html

【hdu 1533】Going Home相关推荐

  1. 【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

    题干: On a grid map there are n little men and n houses. In each unit time, every little man can move ...

  2. 大数加法【HDU 1002】

    大数加法模板 一般的加法只要int类型的两数直接相加即可,大一点的数可以设为long long类型,而超过长整型的数则属于大数问题了,大数加法其实也比较简单,利用数组实现就可以啦: 主要思想如下: ( ...

  3. 【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)

    题干: Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission ...

  4. 【HDU - 1455】Sticks (dfs + 剪枝)

    题干: George took sticks of the same length and cut them randomly until all parts became at most 50 un ...

  5. 【HDU - 4006】The kth great number (优先队列,求第k大的数)

    题干: Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to wri ...

  6. 【HDU - 4217 】Data Structure? (线段树求第k小数)

    题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...

  7. 【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)

    题干: 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当 ...

  8. 【HDU 5765】Bonds(进制运算妙用)

    [HDU 5765]Bonds(进制运算妙用) Bonds Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K ...

  9. 【HDU 5755】Gambler Bo(高斯消元)

    [HDU 5755]Gambler Bo(高斯消元) Gambler Bo Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072 ...

最新文章

  1. ie8恶心的bug--4个小时的教训
  2. WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.
  3. 关于javascript遍历对象
  4. u3d文件上传至服务器,unity 上传图片到云服务器
  5. 【渝粤题库】陕西师范大学164205 ERP原理及应用 作业(专升本)
  6. 2018年自然语言处理最值得关注的研究、论文和代码
  7. 暴力 Codeforces Round #183 (Div. 2) A. Pythagorean Theorem II
  8. java 字符串方法
  9. python word自动化_python操作word,自动化办公
  10. Laravel 使用firstOrCreate 报错MassAssignmentException
  11. 百度地图隐藏地名_官宣,鲁能公馆是“怪地名”,以后我只是秦新名邸
  12. 例2.12 今年暑假不AC - 九度教程第22题(贪心算法)
  13. IntelliJ IDEA常见问题解决办法汇总
  14. js模板引擎渲染html,JavaScript模板引擎 art-template.js 的使用
  15. 微博 用户画像_面向新浪微博的用户画像研究
  16. 微信公众号添加html,网站中增加微信公众账号链接的方法
  17. 身份认证是计算机网络系统的用户,计算机网络知识:网络认证技术之身份认证技术...
  18. pod2g宣布A5的Sandbox破解成功
  19. 电子宠物小狗-内部结构是什么?
  20. 微信已发图片群里服务器撤回,怎样撤回微信群发的信息?撤不回怎么办?

热门文章

  1. 毕业论文指之 “国内外研究现状”的撰写
  2. DDD的常见问题、争论以及局限性
  3. LoadRunner测试工具大全下载,破解,licence
  4. 7、统计字母、空格、数字 与 其它字符的个数
  5. JavaScript,css时间计时器
  6. 惠普linux进入bios设置u盘启动,hp惠普笔记本进入bios设置u盘启动装系统的方法步骤详细教程 - 系统家园...
  7. 怎么选择合适的机柜?网络机柜服务器机柜
  8. idc机房数据中心租赁机柜的优势
  9. iphone mac平台下破解微信的语音
  10. 计算机Word多选题试题,计算机一级考试Word试题