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

Going Home

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


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
 


题意:把H和m一一配对,他们所需走的最小步数和 为多少。


做法:用bfs 先找出 任意 H 和 m 直接的最小步数。记录下来。 然后像二分图一样建图。 起点到 所有home 流量1,费用0。 home 和man 之间的费用为距离,流量1,man和终点ee之间流量1,费用0。 建图完,然后用最小费用最大流 跑一边就ok了。


#include <cstdio>
#include <iostream> 
#include <cmath>
#include <cstring>
#include <vector>
#include <stdlib.h>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;


const int MAXN = 10100;
const int MAXM = 30000000;
const int INF = 0x3f3f3f3f;
struct Edge
{
	int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
	N = n;
	tol = 0;
	memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++; 
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
bool spfa(int s,int t)
{
	queue<int>q;
	for(int i = 0;i < N;i++)
	{
		dis[i] = INF;
		vis[i] = false;
		pre[i] = -1;
	}
	dis[s] = 0;
	vis[s] = true;
	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)
		{
			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] == -1)return false;
	else return true;
}
//返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
	int flow = 0;
	cost = 0;
	while(spfa(s,t))
	{
		int Min = INF;
		for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
		{
			if(Min > edge[i].cap - edge[i].flow)
				Min = edge[i].cap - edge[i].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;
	}
	return flow;
} 

int n,m;
struct node
{
	int x,y,bu;
};
//addedge(int u,int v,int cap,int cost)
char mp[110][110];//地图
char num[110][110];//点查 号
int mp_dis[110][110];//号 号距离  前 home  后 men
int mp_vis[110][110];
int dir[4][2]={1,0,0,1,0,-1,-1,0};
void bfs(int x,int y)
{
	memset(mp_vis,-1,sizeof mp_vis);
	queue<node> q;
	node sta,nw,tem;
	sta.bu=0;
	sta.x=x;
	sta.y=y;
	q.push(sta);

	while(!q.empty())
	{
		nw=q.front();
		q.pop();
		if(mp[nw.x][nw.y]=='m')
			mp_dis[num[x][y]][num[nw.x][nw.y]]=nw.bu;
		for(int i=0;i<4;i++)
		{
			int xx=nw.x+dir[i][0];
			int yy=nw.y+dir[i][1];
			if(xx<0||xx>=n||yy<0||yy>=m)
				continue;
			if(mp_vis[xx][yy]!=-1&&nw.bu+1>=mp_vis[xx][yy])
				continue;
			tem.x=xx;
			tem.y=yy;
			mp_vis[xx][yy]=tem.bu=nw.bu+1;  
			q.push(tem); 
		} 
	} 
}

int main()
{
	while(cin>>n>>m,n||m)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%s",mp[i]);
		}
		int home,man;
		home=man=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(mp[i][j]=='H')
					num[i][j]=home++;
				if(mp[i][j]=='m')
					num[i][j]=man++;
			}
		}

		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(mp[i][j]=='H')
					bfs(i,j);

			}
		}
		int ss=home+man;
		int ee=home+man+1;
		init(home+man+2);


		for(int i=0;i<home;i++)
		{
			for(int j=0;j<man;j++)
			{
				addedge(i,home+j,1,mp_dis[i][j]);
			}
		}

		for(int i=0;i<home;i++)
			addedge(ss,i,1,0);
		for(int i=0;i<man;i++)
			addedge(home+i,ee,1,0);

		/*
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{

				printf("%c%d ",mp[i][j],num[i][j]);
			}
			puts("");
		}

		for(int i=0;i<home;i++)
		{
			for(int j=0;j<man;j++)
			{
				printf("%d ",mp_dis[i][j]);
			}
			puts("");
		}
		*/
		int ans;
		minCostMaxflow(ss,ee,ans);

		printf("%d\n",ans);



	}
	return 0;
}

/*
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
*/









数据治理是确保数据准确性、可靠性、安全性、可用性和完整性的体系和框架。它定义了组织内部如何使用、存储、保护和共享数据的规则和流程。数据治理的重要性随着数字化转型的加速而日益凸显,它能够提高决策效率、增强业务竞争力、降低风险,并促进业务创新。有效的数据治理体系可以确保数据在采集、存储、处理、共享和保护等环节的合规性和有效性。 数据质量管理是数据治理中的关键环节,它涉及数据质量评估、数据清洗、标准化和监控。高质量的数据能够提升业务决策的准确性,优化业务流程,并挖掘潜在的商业价值。随着大数据和人工智能技术的发展,数据质量管理在确保数据准确性和可靠性方面的作用愈发重要。企业需要建立完善的数据质量管理和校验机制,并通过数据清洗和标准化提高数据质量。 数据安全与隐私保护是数据治理中的另一个重要领域。随着数据量的快速增长和互联网技术的迅速发展,数据安全与隐私保护面临前所未有的挑战。企业需要加强数据安全与隐私保护的法律法规和技术手段,采用数据加密、脱敏和备份恢复等技术手段,以及加强培训和教育,提高安全意识和技能水平。 数据流程管理与监控是确保数据质量、提高数据利用率、保护数据安全的重要环节。有效的数据流程管理可以确保数据流程的合规性和高效性,而实时监控则有助于及时发现并解决潜在问。企业需要设计合理的数据流程架构,制定详细的数据管理流程规范,并运用数据审计和可视化技术手段进行监控。 数据资产管理是将数据视为组织的重要资产,通过有效的管理和利用,为组织带来经济价值。数据资产管理涵盖数据的整个生命周期,包括数据的创建、存储、处理、共享、使用和保护。它面临的挑战包括数据量的快速增长、数据类型的多样化和数据更新的迅速性。组织需要建立完善的数据管理体系,提高数据处理和分析能力,以应对这些挑战。同时,数据资产的分类与评估、共享与使用规范也是数据资产管理的重要组成部分,需要制定合理的标准和规范,确保数据共享的安全性和隐私保护,以及建立合理的利益分配和权益保障机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值