首页 技术 正文
技术 2022年11月9日
0 收藏 739 点赞 2,907 浏览 1974 个字

http://poj.org/problem?id=2195

题意 :  N*M的点阵中,有N个人,N个房子。让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之后,需要花多少美元。

思路 :最小费用最大流。把人作为一个顶点集合U,房子作为另一个顶点集合V,把U中所有点到V中所有点连线,费用cost[u][v]为abs(△x)+abs(△y),反向弧费用cost[v][u]= -cost[u][v],容量cap[u][v]=1,构成一个多源多汇的二分图。 由于每一个多源多汇的网络流都必有一个与之对应的单源单汇的网络流,为了便于解题,由此构造一个超级源s和超级汇t,超级源s与U中所有点相连,费用cost[s][u]=0(这是显然的),容量cap[s][u]=1;V中所有点与超级汇t相连,费用cost[v][t]=0(这是显然的),容量cap[t][v]=1。至于其他不连通的点,费用与容量均为0。容量为0的边,可以理解为饱和边,不再连通。而上述的所有边之所以容量初始化为1,是因为每间房子只允许入住1个人。而与超级源(汇)相连的边的费用之所以为0,是为了现在所构造的单源单汇网络流最终所求的最小费用等于原来的多源多汇网络流的最小费用。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <math.h>using namespace std;const int maxn = ;
struct node
{
int u ;
int v ;
}p[],h[] ;int pre[maxn],dist[maxn],n,m,pn,hn,s,t,cnt;
int cap[maxn][maxn],flow[maxn][maxn],cost[maxn][maxn] ;
bool flag[maxn] ;
char ch[maxn][maxn] ;const int INF = ;void Init()
{
pn = hn = ;
cnt = s = ;
memset(cap,,sizeof(cap)) ;
memset(flow,,sizeof(flow)) ;
memset(cost,,sizeof(cost)) ;
}
void spfa()
{
queue<int>Q ;
for(int i = ; i < maxn ; i++)
dist[i] = INF ;
memset(pre,-,sizeof(pre)) ;
memset(flag,false,sizeof(flag)) ;
Q.push(s) ;
flag[s] = true ;
dist[s] = ;
while(!Q.empty())
{
int u = Q.front() ;
Q.pop() ;
flag[u] = false ;
for(int v = ; v <= t ; v++)
{
if(cap[u][v] && dist[v] > dist[u] + cost[u][v])
{
dist[v] = dist[u] + cost[u][v] ;
pre[v] = u ;
if(!flag[v])
{
Q.push(v) ;
flag[v] = true ;
}
}
}
}
}void mcmf()
{
for( ; ; )
{
spfa() ;
if(pre[t] == -) break ;//没有父节点了,
int x = t ,minn = INF ;
while(pre[x] != -)
{
minn = min(minn,cap[pre[x]][x]) ;
x = pre[x] ;
}
x = t ;
while(pre[x] != -)
{
cap[pre[x]][x] -= minn ;
cap[x][pre[x]] += minn ;
cnt += minn*cost[pre[x]][x];
x = pre[x];
}
}}int main()
{
while(scanf("%d %d",&n,&m) != EOF)
{
if(n == && m == ) break ;
Init() ;
for(int i = ; i < n ; i++)
{
scanf("%s",ch[i]) ;
for(int j = ; j < m ; j++)
{
if(ch[i][j] == 'H')
{
h[++hn].u = i ;
h[hn].v = j ;
}
else if(ch[i][j] == 'm')
{
p[++pn].u = i ;
p[pn].v = j ;
}
}
}
t = pn+hn+ ;
for(int i = ; i <= pn ; i++)
cap[s][i] = ;
for(int i = ; i <= hn ; i++)
cap[i+pn][t] = ;
for(int i = ; i <= pn ; i++)
{
for(int j = ; j <= hn ; j++)
{
cap[i][j+pn] = ;
cost[i][j+pn] = fabs(p[i].u-h[j].u) + fabs(p[i].v-h[j].v) ;
cost[j+pn][i] = -cost[i][j+pn] ;
}
}
mcmf() ;
printf("%d\n",cnt) ;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900