HD 1533 Going Home(最小费用最大流模板)
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3666 Accepted Submission(s):
1884
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.
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.
integer, which is the minimum amount, in dollars, you need to pay.
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
10
28
...H....
...H....
...H....
mmmHmmmm
以所有H 到 所有m 连一条边,边的权重为两者者距离,然后加一个超级源点和汇点,与原点的流量为1,费用为0,汇点也是一样。
转换成了求嘴小费用最大流问题;
//刘汝佳模板
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const int MAXN = 400;
const int MAXM = 200000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from,to,cap,cost,flow;
Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),cost(f),flow(w){}
};
vector<Edge> edge;
vector<int> g[MAXN];
int inq[MAXN],d[MAXN],p[MAXN],a[MAXN];
int NN,MM;
int topH,topP;
struct point
{
int x,y;
}H[MAXN],P[MAXN]; void input()
{
char ch;
topH = topP = 0;
for(int i = 1; i <= NN; i++)
{
for(int j = 1; j <= MM; j++)
{
scanf("%c",&ch);
if(ch == 'H')
{
topH++;
H[topH].x = i;
H[topH].y = j;
}
else if(ch == 'm')
{
topP++;
P[topP].x = i;
P[topP].y = j;
}
}
getchar();
}
}
void AddEdge(int from, int to, int cap, int cost)
{
edge.push_back(Edge(from,to,cap,cost,0));
edge.push_back(Edge(to,from,0,-cost,0));
int m = edge.size();
g[from].push_back(m - 2);
g[to].push_back(m - 1);
}
int MCMF(int s,int t,int& flow, int& cost)
{ for(int i = 0; i < MAXN; i++)
d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0;
inq[s] = 1;
p[s] = 0;
a[s] = INF; queue<int> myque;
myque.push(s);
while(myque.empty() == 0)
{
int u = myque.front();
myque.pop();
inq[u] = 0;
for(int i = 0; i < (int)g[u].size(); i++)
{
Edge e = edge[ g[u][i] ];
if(e.cap > e.flow && 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 - e.flow);
if(inq[e.to] == 0)
{
myque.push(e.to);
inq[e.to] = 1;
}
}
}
}
if(d[t] == INF)
return false;
flow += a[t];
cost += d[t] * a[t]; for(int u = t; u != s; u = edge[ p[u] ].from)
{
edge[ p[u] ].flow += a[t];
edge[ p[u] ^ 1].flow -= a[t];
}
return true; }
int creatGraph()
{
int ans = 0,flow = 0;
int MN = topH + topP;
for(int i = 1; i <= topP; i++)
{
for(int j = 1; j <= topH; j++)
{
int t = abs(H[i].x - P[j].x) + abs(H[i].y - P[j].y); //距离最为费用
AddEdge(i,topP + j, 1, t); // 边i到topP+j,把所有H.m点都排号序号
}
} for(int i = 1; i <= topP; i++)
AddEdge(MN + 1, i, 1, 0); //源点到M点
for(int i = topP + 1; i <= MN; i++)
AddEdge(i, MN + 2, 1,0); // H点到汇点
while(MCMF(MN + 1, MN + 2, flow, ans) );
return ans;
}
int main()
{
while(scanf("%d%d",&NN,&MM) != EOF)
{
if(NN == 0 && MM == 0)
break;
getchar();
for(int i = 1; i < MAXN; i++)
g[i].clear();
edge.clear();
input();
printf("%d\n",creatGraph());
}
return 0;
}
HD 1533 Going Home(最小费用最大流模板)的更多相关文章
- POJ 2195 & HDU 1533 Going Home(最小费用最大流)
这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
- HDU3376 最小费用最大流 模板2
Matrix Again Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)To ...
- 洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
- 最大流 && 最小费用最大流模板
模板从 这里 搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...
- 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 ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- hdu 1533 Going Home 最小费用最大流 入门题
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- luogu 3376 最小费用最大流 模板
类似EK算法,只是将bfs改成spfa,求最小花费. 为什么可以呢,加入1-3-7是一条路,求出一个流量为40,那么40*f[1]+40*f[2]+40*f[3],f[1]是第一条路的单位费用,f[2 ...
- POJ2135 最小费用最大流模板题
练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...
随机推荐
- Python进阶-面向对象
类的成员 类的成员可以分为三类:字段.方法.属性 一:字段: 普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同 普通字段属于对象 静态字段属于类 字段的定义和使用 ...
- 限制站点目录防止跨站的三种方案(使用open_basedir)
nginx结合php的时候,可以使用open_basedir限制站点目录防止跨站具体实现方法有以下三种:注意:以下三种设置方法均需要PHP版本为5.3或者以上. 方法1)在Nginx配置文件中加入fa ...
- CSS3弹性伸缩布局(一)——box布局
CSS3弹性伸缩布局简介 2009年,W3C提出了一种崭新的方案----Flex布局(即弹性伸缩布局),它可以简便.完整.响应式地实现各种页面布局,包括一直让人很头疼的垂直水平居中也变得很简单地就迎刃 ...
- hammer.js中文文档
转自:http://www.uedsc.com/hammerjs-api.html HammerJS是一个优秀的.轻量级的触屏设备手势库,现在已经更新到2.04版本,跟1.0版本有点天壤地别了,毕竟改 ...
- codevs 1133 表达式的值
1133 表达式的值 2011年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descript ...
- JAVA 根据数据库表内容生产树结构JSON数据
1.利用场景 组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段 2.构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据) List ...
- python package 的两种组织方式
方式一/package1/ .../__init__.py # 空文件 .../class1.py class Class1: def __init__(self): self.name = &quo ...
- Hello,cnblog‘s world!
纠结了许久,终于选在这个地方安家.之所以选在这里,是因为这里是个很干净的社区. 之前一直在其他博文网站里发文章,可是越到后来,发现页面广告越来越多.真正对自己有价值的内容越来越少,虽然已经使用过几年了 ...
- 如何下载struts 2及其各个包的作用
一.http://archive.apache.org/dist/struts/library/ 二. struts官网: http://struts.apache.org/ 进入主页后点击" ...
- Vs2012 中使用itoa
自己在写程序的时候经常用到保存大量的图片,从而对其编号,所以要把整型转换成字符型. 通常自己定义string,而字符使用char[],把整形转换成char类型,然后和string类型相加,但是在VS2 ...