HDU-1533 Going Home

KM模版题。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iostream>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define N 123
#define MAX 1<<30
#define ll long long
#define clr(x, c) memset(x, c, sizeof(x))
using namespace std;
int read()
{
	int x=0, f=1; char ch=getchar();
	while (ch<‘0‘ || ch>‘9‘) { if (ch==‘-‘) f=-1; ch=getchar(); }
	while (ch>=‘0‘ && ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); }
	return x*f;
}

struct point{int x, y;} wx[N], wy[N];
int n, m, nx, ny, v[N][N], l[N], st[N], lx[N], ly[N];
bool vx[N], vy[N];
char s[N];

bool Find(int x)
{
	vx[x]=1;
	rep(y, 1, ny) if (!vy[y])
	{
		int a=lx[x]+ly[y]-v[x][y];
		if (!a)
		{
			vy[y]=1; if (!l[y] || Find(l[y])) { l[y]=x; return true; }
		}
		else st[y]=min(st[y], a);
	}
	return false;
}

int KM()
{
	clr(ly, 0); clr(l, 0);
	rep(i, 1, nx) lx[i]=-MAX;
	rep(i, 1, nx) rep(j, 1, ny) if (v[i][j]>lx[i]) lx[i]=v[i][j];
	rep(i, 1, nx)
	{
		rep(j, 1, ny) st[j]=MAX;
		while (1)
		{
			clr(vx, 0); clr(vy, 0);
			if (Find(i)) break;
			int a=MAX; rep(j, 1, ny) if (!vy[j] && a>st[j]) a=st[j];
			rep(j, 1, nx) if (vx[j]) lx[j]-=a;
			rep(j, 1, ny) if (vy[j]) ly[j]+=a; else st[j]-=a;
		}
	}
	int ans=0;
	rep(i, 1, ny) ans+=v[l[i]][i];
	return ans;
}

int main()
{
	while (~scanf("%d%d", &n, &m) && (n||m))
	{
		nx=ny=0;
		rep(i, 1, n)
		{
			scanf("%s", s);
			rep(j, 1, m)
				if (s[j-1]==‘m‘) nx++, wx[nx].x=i, wx[nx].y=j;
				else if (s[j-1]==‘H‘) ny++, wy[ny].x=i, wy[ny].y=j;
		}
		rep(i, 1, nx) rep(j, 1, ny) v[i][j]=-(abs(wx[i].x-wy[j].x)+abs(wx[i].y-wy[j].y));
		printf("%d\n", -KM());
	}
	return 0;
}

  

时间: 2024-10-16 12:34:08

HDU-1533 Going Home的相关文章

HDU 1533 Going Home(KM完美匹配)

HDU 1533 Going Home 题目链接 题意:就是一个H要对应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,由于是要最小,所以边权建负数来处理即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXNODE = 105; typedef int Type; const T

POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 MCMF算法,我写的是MCMF算法,一开始想的是连10000个点,但是不会连那些大众点之间的边,只会连超级点和普通点之间的边.后来觉得只要连房子点和 人点就可以了.连从人到房子的边,容量是1,花费是他们之间的曼哈顿距离,然后超级源点和超级汇点像上面那样连接,注意连点的时候把他们每个点都具体化一下,就

hdu 1533 Going Home (KM算法)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2715    Accepted Submission(s): 1366 Problem Description On a grid map there are n little men and n houses. In each unit time, every

【HDU 1533】 Going Home (KM)

Going Home 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 f

Going Home HDU - 1533 (费用流)

Going Home HDU - 1533 1 //费用流初探 2 #include <iostream> 3 #include <queue> 4 #include <cstring> 5 #include <cstdio> 6 #include <algorithm> 7 using namespace std; 8 const int inf = 0x3f3f3f3f; 9 const int maxn = 110; 10 char gra

POJ 2195 &amp; HDU 1533 Going Home(最小费用最大流)

题目链接: POJ:http://poj.org/problem?id=2195 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1533 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,

hdu 1533 Going Home 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 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

HDU 1533 最小费用最大流(模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void init(int n) 这个函数一定要写 一开始忘记写这个WA了好几发 还有这个题很容易T掉,赋值建图要简化,一开始构建成网络流那种图一直T #include <stdio.h> #include <string.h> #include <iostream> #includ

HDU 1533 Going Home(最小费用流)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3278    Accepted Submission(s): 1661 Problem Description On a grid map there are n little men and n houses. In each unit time, every

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

嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(House)作为汇点,各个源点与汇点分别连一条边,这条边的流量是1(因为每个源点只能走一条边到汇点),费用是 从源点走到汇点的步数,因为有多个源点与汇点,要建一个超级源点与超级汇点,超级源点与各个源点连一条流量为1,费用为0(要避免产生多余的费用)的边 按照这个图跑一发费用流即可 把代码挂上去,用的是前向星写的 1 #include<cstdio> 2 #include<cstr