Going Home

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

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

题意  n个人 n个房子 在N*M个方格  人移动一格要花费 1(只能水平竖直四个方向)  问n个人走到n个房子的最小花费是多少(一个房子只能一个人待)

解析  这道题可以转化成费用流来解决,n个源点n个汇点 最大流为n的最小费用 ,我们直接建立一个超源点0,一个超汇点n*m+1 然后和源点汇点相连 容量1 费用0

注意 其他边的费用为1 但是容量要设为inf 因为走过之后还可以走.

也可以用二分图最大全匹配写 还没get这项技能。。。

代码一   // 一比二快100ms。

 #include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+,mod=1e9+,inf=0x3f3f3f3f;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
int dir[][]={{,},{-,},{,-},{,}};
char a[maxn][maxn];
struct MCMF {
struct Edge {
int from, to, cap, cost;
Edge(int u, int v, int w, int c): from(u), to(v), cap(w), cost(c) {}
};
int n, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn], d[maxn], p[maxn], a[maxn]; void init(int n) {
this->n = n;
for (int i = ; i <= n; i ++) G[i].clear();
edges.clear();
}
void addedge(int from, int to, int cap, int cost) {
edges.push_back(Edge(from, to, cap, cost));
edges.push_back(Edge(to, from, , -cost));
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
}
bool BellmanFord(int s, int t, int &flow, int &cost) {
for (int i = ; i <= n; i ++) d[i] = inf;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = inf; queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = ;
for (int i = ; i < G[u].size(); i ++) {
Edge &e = edges[G[u][i]];
if (e.cap && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap);
if (!inq[e.to]) {
Q.push(e.to);
inq[e.to] = ;
}
}
}
}
if (d[t] == inf) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].cap -= a[t];
edges[p[u] ^ ].cap += a[t];
u = edges[p[u]].from;
}
return true;
}
int solve(int s, int t) {
int flow = , cost = ;
while (BellmanFord(s, t, flow, cost));
return cost;
}
}solver;;
void build(int n,int m)
{
vector<int> ss,tt;
for(int i=;i<=n;i++)
{
scanf("%s",a[i]+);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
int temp=(i-)*m+j;
if(a[i][j]=='m')
ss.push_back(temp);
else if(a[i][j]=='H')
tt.push_back(temp);
for(int k=;k<;k++)
{
int x=i+dir[k][];
int y=j+dir[k][];
if(x>=&&x<=n&&y>=&&y<=m)
{
int temp2=(x-)*m+y;
solver.addedge(temp,temp2,inf,);
// cout<<i<<" "<<j<<" "<<temp<<" "<<temp2<<endl;
}
}
}
}
for(int i=;i<ss.size();i++)
solver.addedge(,ss[i],,);
for(int i=;i<tt.size();i++)
solver.addedge(tt[i],n*m+,,);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&n&&m)
{
solver.init(n*m+);
build(n,m);
int maxflow;
maxflow=solver.solve(,n*m+);
printf("%d\n",maxflow);
}
}

代码二

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2e4+,mod=1e9+,inf=0x3f3f3f3f;
struct edge
{
int to,next,cap,flow,cost;
} edge[maxn*];
int head[maxn],tol;
int pre[maxn],dis[maxn];
bool vis[maxn];
int N;
char a[maxn][maxn];
void init(int n)
{
N=n,tol=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].flow=;
edge[tol].cost=cost;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].flow=;
edge[tol].cost=-cost;
edge[tol].next=head[v];
head[v]=tol++;
}
bool spfa(int s,int t)
{
queue<int> q;
for(int i=; i<=N; i++)
{
dis[i]=inf;
vis[i]=false;
pre[i]=-;
}
dis[s]=;
vis[s]=true;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
else return true;
}
int mincostflow(int s,int t,int &cost)
{
int flow=;
cost=;
while(spfa(s,t))
{
int Min=inf;
for(int i=pre[t]; i!=-; i=pre[edge[i^].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
}
for(int i=pre[t]; i!=-; i=pre[edge[i^].to])
{
edge[i].flow+=Min;
edge[i^].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
}
return flow;
}
int dir[][]={{,},{-,},{,-},{,}};
void build(int n,int m)
{
vector<int> ss,tt;
for(int i=;i<=n;i++)
{
scanf("%s",a[i]+);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
int temp=(i-)*m+j;
if(a[i][j]=='m')
ss.push_back(temp);
else if(a[i][j]=='H')
tt.push_back(temp);
for(int k=;k<;k++)
{
int x=i+dir[k][];
int y=j+dir[k][];
if(x>=&&x<=n&&y>=&&y<=m)
{
int temp2=(x-)*m+y;
addedge(temp,temp2,inf,);
// cout<<i<<" "<<j<<" "<<temp<<" "<<temp2<<endl;
}
}
}
}
for(int i=;i<ss.size();i++)
addedge(,ss[i],,);
for(int i=;i<tt.size();i++)
addedge(tt[i],n*m+,,);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&n&&m)
{
init(n*m+);
build(n,m);
int ans,maxflow;
maxflow=mincostflow(,n*m+,ans);
printf("%d\n",ans);
}
}

HDU1533 最小费用最大流的更多相关文章

  1. hdu1533 Going Home 最小费用最大流 构造源点和汇点

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

  2. [hdu1533]二分图最大权匹配 || 最小费用最大流

    题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...

  3. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  4. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  5. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  8. 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 821  Solved: 502[Submit][Status ...

  9. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

随机推荐

  1. laravel中的队列

    Laravel 队列为不同的后台队列服务提供统一的 API,可使用多种驱动,eg:mysql,redis,Beanstalkd等,驱动已经封装,不需要管理这些驱动,只需要修改配置就可以更改驱动,在驱动 ...

  2. HYSBZ 1503 郁闷的出纳员 (Splay树)

    题意: 作为一名出纳员,我的任务之一便是统计每位员工的工资.但是我们的老板反复无常,经常调整员工的工资.如果他心情好,就可能把每位员工的工资加上一个相同的量.反之,如果心情不好,就可能把他们的工资扣除 ...

  3. 洛谷 P2894 [USACO08FEB]酒店Hotel

    题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  4. 洛谷 P2788 数学1(math1)- 加减算式

    题目背景 蒟蒻HansBug在数学考场上,挠了无数次的头,可脑子里还是一片空白. 题目描述 好不容易啊,HansBug终于熬到了做到数学最后一题的时刻了,眼前是一堆杂乱的加减算式.显然成功就在眼前了. ...

  5. Asp.Net Core 入门(一)——Program.cs做了什么

    ASP.NET Core 是微软推出的一种全新的跨平台开源 .NET 框架,用于在 Windows.Mac 或 Linux 上生成基于云的新式 Web 应用程序.国内目前关于Asp.Net Core的 ...

  6. stay hungry stay foolish.

    I am honored to be with you today at your commencement from one of the finest universities in the wo ...

  7. 尺取法 || POJ 2739 Sum of Consecutive Prime Numbers

    给一个数 写成连续质数的和的形式,能写出多少种 *解法:先筛质数 然后尺取法 **尺取法:固定区间左.右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点               ...

  8. 基于HLS(HTTP Live Streaming)的视频直播分析与实现

    转自:http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html HLS(HTTP Live Streaming)的分析: HTT ...

  9. 【问题探索日志】python 函数优化

    # 事情是这样的,我写的一个程序帧率上不去. 然后发现了一个疑似有问题的地方,如下 def around(x,y): around_dict = {(i,j) for i in range(-1,2) ...

  10. 剑指Offer(书):从尾到头打印链表

    题目:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 分析:若不允许修改原链表的值,则可以使用栈来实现,也可以使用另外一个ArrayList做中转的数据. public ArrayL ...