Going Home

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

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
题意:一群人在操场玩耍,突然下起了大雨,大家匆忙找房子躲雨,每走一步会消耗一卡路里,问所有人躲到房子里面最少会花费多少卡路里。
m代表人,H代表房子,一间房子只能容纳一个人,如果房子里面有人,其他人可以从房子经过,但躲不了雨。
 
题解:输入中的每个人和房子可以组成一条条边,我们用一个源点和汇点把所有边统一起来,组成一个网络图,在这个网络图中求需要的最小花费
 
 
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
#define mx 0x3f3f3f3f
using namespace std;
int cap[][],cost[][],flow[][];//cap是容量,cost是花费,flow是流量
int pre[],dis[],vis[],cnt[];//pre是记录增广路的前驱节点,dis[i]是记录源点到节点i的最小距离
//vis[i]标记点是否在队列中,cnt[i]记录点i进入队列的次数
char str[][];
struct node
{
int x;
int y;
}s[],e[]; int n,m;
int st,endd,nn,mm;//st是源点,endd是汇点
int mn_cost,mx_flow;
int spfa()
{
for(int i=;i<n;i++)
dis[i]=mx;
memset(vis,,sizeof(vis));
memset(cnt,,sizeof(cnt)); queue<int>p;
p.push(st);//st是起点
vis[st]=;
cnt[st]=;
dis[st]=;
while(!p.empty())
{
int now=p.front();
p.pop();
vis[now]=;
for(int i=;i<n;i++)
{
if(cap[now][i]>flow[now][i]&&dis[i]>dis[now]+cost[now][i])//这里将费用看成是长度,求源点到汇点的最小距离
{
dis[i]=dis[now]+cost[now][i];
pre[i]=now;
if(vis[i]==)
{
p.push(i);
vis[i]=;
cnt[i]++;
if(cnt[i]>n)
return ;
}
}
}
}
if(dis[endd]>=mx)
return ;
return ;
}
void bfs(int n)//顶点数
{
memset(flow,,sizeof(flow));
mx_flow=;
mn_cost=;
while(spfa())
{
int f=mx;
for(int i=endd;i!=st;i=pre[i])
f=min(f,cap[pre[i]][i]-flow[pre[i]][i]); for(int i=endd;i!=st;i=pre[i])//更新流量
{
flow[pre[i]][i]+=f;
flow[i][pre[i]]-=f;
}
mn_cost+=dis[endd]*f;
mx_flow+=f; }
}
int main()
{
while(~scanf("%d%d",&nn,&mm)&&nn&&mm)
{
//这道题需要自己将输入转化,建立一个有向图
int cnt1=,cnt2=;
for(int i=;i<nn;i++)
{
scanf("%s",str[i]);
for(int j=;j<mm;j++)
{
if(str[i][j]=='m')//起点
{
cnt1++;
s[cnt1].x=i;
s[cnt1].y=j;
}
if(str[i][j]=='H')//终点
{
cnt2++;
e[cnt2].x=i;
e[cnt2].y=j;
}
}
}
n=cnt1+cnt2+;//加上源点和汇点,共有n个点,[0,n-1]
st=;//源点
endd=cnt1+cnt2+;//汇点
memset(cap,,sizeof(cap));
memset(cost,,sizeof(cost));
for(int i=;i<=cnt1;i++)//初始化源点到任意起点的花费为0,容量为1;
{
cap[][i]=;
cost[][i]=;
cost[i][]=;
}
for(int i=;i<=cnt2;i++)//初始化汇点到所有终点的花费为0,容量为1;
{
cap[i+cnt1][endd]=;
cost[i+cnt1][endd]=;
cost[endd][i+cnt1]=;
}
for(int i=;i<=cnt1;i++)//初始化起点到终点的花费和容量
{
for(int j=;j<=cnt2;j++)
{
cap[i][cnt1+j]=;
cost[i][cnt1+j]=abs(s[i].x-e[j].x)+abs(s[i].y-e[j].y);
cost[cnt1+j][i]=-cost[i][cnt1+j];
}
}
bfs(n);
printf("%d\n",mn_cost);
}
return ;
}

hdu 1533 Going Home 最小费用最大流 (模板题)的更多相关文章

  1. hdu 1533 Going Home 最小费用最大流 入门题

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. POJ 2195 & HDU 1533 Going Home(最小费用最大流)

    这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...

  3. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  4. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  5. POJ2135 最小费用最大流模板题

    练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...

  6. 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]

    题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  7. hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  9. 图论算法-最小费用最大流模板【EK;Dinic】

    图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...

随机推荐

  1. Codeforces Round #592 (Div. 2)G(模拟)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...

  2. 【PAT甲级】1052 Linked List Sorting (25 分)

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

  3. CentOS上安装elasticsearch

    1.安装docker yum install docker 2.启动docker systemctl start docker 查看docker版本: docker -v 之后只要启动机器就会自动开启 ...

  4. java中关于&0xFF 的问题

    最近遇到一个问题,半天也没想明白,byte temp = 0xA0,为什么System.out.println(temp),打印的值为:-96:而System.out.println(temp& ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:电脑程序输出: Sample output

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 微信小程序引用外部js,引用外部样式,引用公共页面模板

    https://blog.csdn.net/smartsmile2012/article/details/83414642 ================小程序引用外部js============= ...

  7. Centos7 iptables firewalld防火墙与selinux配置

    一.iptables防火墙 1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service ...

  8. Python 爬取 北京市政府首都之窗信件列表-[数据处理]

    日期:2020.01.24 博客期:132 星期五 [代码说明,如果要使用此页代码,必须在本博客页面评论区给予说明] //博客总体说明 1.准备工作 2.爬取工作 3.数据处理(本期博客) 4.信息展 ...

  9. ubuntu18.04下安装node

    安装Node.js Ubuntu 18.04在其默认存储库中包含一个版本的Node.js,可用于在多个系统间提供一致的体验. 在撰写本文时,存储库中的版本是8.10.0. 这不会是最新的版本,但它应该 ...

  10. Javaweb实现简单的选课系统(主要技术包括jsp、Servlet、Javabean)

    一.背景信息: 首先,这个选课系统是上周一老师在课堂上让我们做的测试,考试时长三个小时,我只做了一半,也没有实现选课流程. 由于上周忙于写实验报告没有时间继续完成这个测试.这周用前天和昨天一共七个小时 ...