Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6114    Accepted Submission(s): 3211

Problem Description
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 

You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

 
Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
 
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay. 
 
Sample Input
2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
 
Sample Output
2
10
28
 
大意:将两个点配对的花费为其曼哈顿距离,问将每个H与m配对的最小花费。
 
 
 
 
 
 
 
题解:建二分图,带权匹配(KM暂时不会)。
一切皆可网络流,也可以用费用流写,这题就当存个模板了。
 
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=0,ff=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
return xx*ff;
}
const int maxlongint=(1LL<<31)-1;
inline int myabs(int xx)
{if(xx<0)return -xx;return xx;}
inline int mymin(int xx,int yy)
{if(xx>yy)return yy;return xx;}
inline int mymax(int xx,int yy)
{if(xx>yy)return xx;return yy;}
int N,M,st,en;
int s1[110],s2[110],tp1,tp2,ans;
inline int get_id(int xx,int yy)
{return (xx-1)*M+yy;}
inline int get_dis(int id1,int id2){
int sx=id1/M,sy=id1%M,fx=id2/M,fy=id2%M;
if(!sy)
sx--,sy=M;
if(!fy)
fx--,fy=M;
return myabs(sx-fx)+myabs(sy-fy);
}
int lin[210],len;
struct edge{
int y,next,v,f;
}e[200010];
inline void insert(int xx,int yy,int ff,int vv){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
e[len].v=vv;
e[len].f=ff;
}
inline void ins(int xx,int yy,int ff,int vv)
{insert(xx,yy,ff,vv),insert(yy,xx,0,-vv);}
void build(){
st=tp1+tp2+1,en=st+1;
memset(lin,0,sizeof(lin));len=0;
for(int i=1;i<=tp1;i++)
for(int j=1;j<=tp2;j++)
ins(i,j+tp1,1,get_dis(s1[i],s2[j]));
for(int i=1;i<=tp1;i++)
ins(st,i,1,0);
for(int j=1;j<=tp2;j++)
ins(j+tp1,en,1,0);
}
int q[1000010],head,tail,dis[210],Prev[210],useedge[210];
bool vis[210];
bool SPFA(){
memset(vis,0,sizeof(vis));
memset(dis,10,sizeof(dis));
head=tail=0;
q[head]=st;
vis[q[head]]=1;
dis[q[head]]=0;
for(;head<=tail;head++){
vis[q[head]]=0;
for(int i=lin[q[head]];i;i=e[i].next)
if(e[i].f)
if(dis[e[i].y]>dis[q[head]]+e[i].v){
dis[e[i].y]=dis[q[head]]+e[i].v;
if(!vis[e[i].y]){
vis[e[i].y]=1;
q[++tail]=e[i].y;
}
Prev[e[i].y]=q[head];
useedge[e[i].y]=i;
}
}
//printf("#%d#\n",dis[en]);
return dis[en]!=dis[0];
}
void agu(){
int add=maxlongint;
for(int i=en;i!=st;i=Prev[i]){
add=mymin(add,e[useedge[i]].f);
}
for(int i=en;i!=st;i=Prev[i]){
e[useedge[i]].f-=add;
if(useedge[i]&1)
e[useedge[i]+1].f+=add;
else
e[useedge[i]-1].f+=add;
ans+=add*e[useedge[i]].v;
}
}
void cost_flow(){
ans=0;
while(SPFA())
agu();
printf("%d\n",ans);
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
while(1){
N=read(),M=read();
if((!N)&&(!M))
break;
tp1=tp2=0;
char tmp;
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++){
tmp=getchar();
while(tmp==10||tmp==32)
tmp=getchar();
if(tmp=='H')
s1[++tp1]=get_id(i,j);
else if(tmp=='m')
s2[++tp2]=get_id(i,j);
}
build();
cost_flow();
}
return 0;
}

  

hdu1533 费用流模板的更多相关文章

  1. HDU2686 费用流 模板

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解

    题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...

  3. 费用流模板(带权二分图匹配)——hdu1533

    /* 带权二分图匹配 用费用流求,增加源点s 和 汇点t */ #include<bits/stdc++.h> using namespace std; #define maxn 1000 ...

  4. 初识费用流 模板(spfa+slf优化) 餐巾计划问题

    今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...

  5. 算法复习——费用流模板(poj2135)

    题目: Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16898   Accepted: 6543 De ...

  6. Spfa费用流模板

    ; ,maxm=; ,fir[maxn],nxt[maxm],to[maxm]; int cap[maxm],val[maxm],dis[maxn],path[maxn]; void add(int ...

  7. zkw费用流模板

    理论:http://www.cnblogs.com/acha/p/6735037.html #include<cstdio> #include<cstring> #includ ...

  8. 【费用流】【Next Array】费用流模板(spfa版)

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...

  9. 费用流 ZOJ 3933 Team Formation

    题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...

随机推荐

  1. 移动web——bootstrap响应式工具

    基本介绍 1.利用媒体查询功能并使用这些工具类可以方便的针对不同设备展示或隐藏页面内容. 基本使用 <!DOCTYPE html> <html lang="zh-CN&qu ...

  2. JS——Boolean(逻辑)对象

    Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true 或者 false). 创建 Boolean 对象的语法: new Boolean(value); //构造函数 Boolean(valu ...

  3. [Windows Server 2012] 手工破解MySQL密码

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:破解MySQL ...

  4. function&箭头函数

    JS中this到底指向谁? function:谁调用指向谁 var id = '654321' var handler = { id: '123456', init: function() { con ...

  5. Capture the Flag ZOJ - 3879(模拟题)

    In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...

  6. python文件头的含义

    一.指定解释器及其路径 在Linux\Mac上,可以用./文件路径直接运行.py文件 这时,需要在python文件开头指定解释器及其路径 #!/usr/bin/python 这样系统就直接按pytho ...

  7. [Usaco2007 Dec]队列变换

    [Usaco2007 Dec]队列变换 题目 FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一 ...

  8. 请问spfa+stack 和spfa+queue 是什么原理

    一个是bfs加迭代 一个是dfs加迭代 请问迭代是什么 就是不断地做,做到没有更优的解为止 或者是不断得做,做到逼近答案为止.. 栈比队列更快更节省空间

  9. [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

    可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...

  10. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...