hdoj--1533--Going Home(最小费用流)
Going Home
Total Submission(s): 3800 Accepted Submission(s): 1948
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.
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.
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
2
10
28
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 200+10
#define MAXM 80000+100
#define INF 0x3f3f3f
struct node
{
int from,to,cap,flow,cost,next;
}edge[MAXM];
int head[MAXN],top,pre[MAXN];
int dis[MAXN],vis[MAXN],n,m,source,sink;
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
struct Node
{
int x,y;
};
Node H[MAXN],M[MAXN];
int m_cnt,h_cnt;
void add(int u,int v,int w,int c)
{
node E1={u,v,w,0,c,head[u]};
edge[top]=E1;
head[u]=top++;
node E2={v,u,0,0,-c,head[v]};
edge[top]=E2;
head[v]=top++;
}
int dist(int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
void getmap()
{
m_cnt=h_cnt=0;
char str[110][110];
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
for(int j=0;j<m;j++)
{
if(str[i][j]=='m')
{
++m_cnt;
M[m_cnt].x=i;
M[m_cnt].y=j;
}
if(str[i][j]=='H')
{
h_cnt++;
H[h_cnt].x=i;
H[h_cnt].y=j;
}
}
}
int k=m_cnt;
source=0;
sink=2*k+1;
for(int i=1;i<=k;i++)
{
add(source,i,1,0);
add(i+k,sink,1,0);
for(int j=1;j<=k;j++)
{
int d=dist(H[i].x,H[i].y,M[j].x,M[j].y);
add(i,j+k,1,d);
}
}
}
bool SPFA(int s,int t)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.to]>dis[u]+E.cost&&E.cap>E.flow)
{
dis[E.to]=dis[u]+E.cost;
pre[E.to]=i;
if(!vis[E.to])
{
vis[E.to]=true;
q.push(E.to);
}
}
}
}
return pre[t]!=-1;
}
void MCMF(int s,int t,int &cost,int &flow)
{
flow=0;
cost=0;
while(SPFA(s,t))
{
int Min=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
node E=edge[i];
Min=min(Min,E.cap-E.flow);
}
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
// printf("%d\n",cost);
}
}
int main()
{
while(scanf("%d%d",&n,&m),n|m)
{
init();
getmap();
int cost,flow;
MCMF(source,sink,cost,flow);
printf("%d\n",cost);
}
return 0;
}
hdoj--1533--Going Home(最小费用流)的更多相关文章
- 最大流增广路(KM算法) HDOJ 1533 Going Home
题目传送门 /* 最小费用流:KM算法是求最大流,只要w = -w就可以了,很经典的方法 */ #include <cstdio> #include <cmath> #incl ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
- hdu 1533 Going Home 最小费用流
构建地图非常easy bfs预处理地图.距离的成本 来源所有m建方,流程1费0 m所有H建方,流程1距离成本 H汇点建设成为各方.流程1费0 #include<cstdio> #inclu ...
- hdoj 1533 Going Home 【最小费用最大流】【KM入门题】
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ的题目分类
模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 10 ...
- hdoj分类
http://blog.csdn.net/lyy289065406/article/details/6642573 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 ...
- HDOJ题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- HDU——PKU题目分类
HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...
- [转] HDU 题目分类
转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
随机推荐
- VHDL之concurrent之block
1 Simple BLOCK The simple block represents only a way of partitioning the code. It allows concurrent ...
- Oracle数据库基础知识总结(一)
数据库名.实例名.数据库域名.全局数据库名.服务名,这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水. 我们现在就来把它们弄个明白. 一.数据库名 什么是数据 ...
- Python3编写自动签到服务程序
公司加班的餐补需要登录网站签到领取,有时候会忘记,于是自己用Python写了小程序来自动签到.刚开始只是做了自己用,直接写的黑框程序,后来给同事用,就打包成exe.再后来有人说要写成window服务会 ...
- asp.net mvc 学习步骤
入门篇(一) ASP.NET MVC案例教程(基于ASP.NET MVC beta) http://www.cnblogs.com/leoo2sk/archive/2008/10/27/1320285 ...
- ORACLE 查询不走索引的原因分析,解决办法通过强制索引或动态执行SQL语句提高查询速度
(一)索引失效的原因分析: <>或者单独的>,<,(有时会用到,有时不会) 有时间范围查询:oracle 时间条件值范围越大就不走索引 like "%_" ...
- MySQL基础命令小结
数据库授权登录[root@localhost ~]# mysql -uroot -p123456mysql> grant select,lock tables on auth.* to 'adm ...
- BZOJ 4259: 残缺的字符串 FFT_多项式
Code: #include<bits/stdc++.h> #define maxn 1200000 using namespace std; void setIO(string s) { ...
- 用doxygen风格注释代码生成文档
目录 用doxygen风格注释代码生成文档 1. 说明 2. 具体操作 2.1 生成头部注释 2.2 安装doxygen 2.3 工程配置 3. 总结 用doxygen风格注释代码生成文档 1. 说明 ...
- js对比for、forEach、map遍历数组速度
function a() { var arr = new Array(1000000); for(var i = 0; i < arr.length;i ++) { arr[i] = i; } ...
- [luogu2474 SCOI2008]天平(floyd差分约束)
传送门 Solution 由于重量只有三种情况,那么想到用差分约束. 由于范围比较小,想到可以floyed求差分约束,暴力求天平另一边 Code #include <cstdio> #in ...