题目大意:一个n*m的地图,上面有一些人man(m)和数量相等的house(H) 图上的距离为曼哈顿距离 问所有人住进一所房子(当然一个人住一间咯)距离之和最短是多少?

思路:一个人一间房,明显是二分图的模型,边权为人和房子的曼哈顿距离,然后算一下最小距离即可 懒得学KM了 最小费用流的经典建图

#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <queue>

#include <iostream>

#define maxn 40000

#define inf 0x3f3f3f3f

using namespace std;

struct POINT

{

int x;

int y;

}a[maxn],b[maxn];

int head[maxn],root[maxn],point[maxn],next[maxn];

int flow[maxn],cost[maxn],pre[maxn],now=0,dist[maxn];

int n,m,h,h2;

char ch[maxn];

int mabs(int x)

{

return x>0?x:-x;

}

int distanc(int i,int j)

{

return mabs(a[i].x-b[j].x)+mabs(a[i].y-b[j].y);

}

void add(int x,int y,int v,int c)

{

next[++now]=head[x];

head[x]=now;

point[now]=y;

flow[now]=v;

cost[now]=c;

root[now]=x;

next[++now]=head[y];

head[y]=now;

point[now]=x;

flow[now]=0;

cost[now]=-c;

root[now]=y;

}

int spfa(int s,int t)

{

memset(pre,0,sizeof(pre));

for(int i=0;i<=t;i++)dist[i]=inf;

dist[s]=0;

int visit[maxn]={0};

visit[s]=1;

queue<int>q;

q.push(s);

while(!q.empty())

{

int u=q.front();

q.pop();

visit[u]=0;

for(int i=head[u];i;i=next[i])

{

int k=point[i];

if(dist[u]+cost[i]<dist[k] && flow[i]!=0)

{

pre[k]=i;

dist[k]=dist[u]+cost[i];

if(!visit[k])

{

visit[k]=1;

q.push(k);

}

}

}

}

return dist[t]!=inf;

}

int main()

{

while(1)

{

scanf("%d%d",&n,&m);

if(n==0&&m==0)break;

now=0;

memset(head,0,sizeof(head));

int ans=0;

h=h2=0;

for(int i=1;i<=n;i++)

{

scanf("%s",ch+1);

for(int j=1;j<=m;j++)

{

if(ch[j]=='H')a[++h].x=i,a[h].y=j;

else if(ch[j]=='m')b[++h2].x=i,b[h2].y=j;

}

}

for(int i=1;i<=h;i++)

{

for(int j=1;j<=h;j++)

{

int u=distanc(i,j);

add(j,i+h+1,1,u);

}

}

int s=h*2+10,t=h*2+11;

for(int i=1;i<=h;i++)add(s,i,1,0);

for(int i=1;i<=h;i++)add(i+h+1,t,1,0);

while(spfa(s,t))

{

int e=pre[t],minx=flow[e];

while(e)

{

if(flow[e]<minx)minx=flow[e];

e=pre[root[e]];

}

e=pre[t];

while(e)

{

flow[e]-=minx;

flow[((e-1)^1)+1]+=minx;

e=[root[e]];

}

ans+=dist[t]*minx;

}

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

}

return 0;

}

POJ 2195 Going Home【最小费用流 二分图最优匹配】的更多相关文章

  1. poj 2195 二分图最优匹配 或 最小费用最大流

    就是最基本的二分图最优匹配,将每个人向每个房子建一条边,权值就是他们manhattan距离.然后对所有权值取反,求一次最大二分图最优匹配,在将结果取反就行了. #include<iostream ...

  2. 51nod 算法马拉松4 D装盒子(网络流 / 二分图最优匹配)

    装盒子   基准时间限制:1 秒 空间限制:131072 KB 分值: 160 有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且 ...

  3. [hdu2255]奔小康赚大钱(二分图最优匹配、KM算法)

    题目大意:求二分图的最优匹配(首先数目最大, 其次权值最大). 解题关键:KM算法 复杂度:$O(n^3)$ #include<cstdio> #include<cstring> ...

  4. POJ 2195 Going Home 【二分图最小权值匹配】

    传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  5. POJ 2195 Going Home 最小费用流 裸题

    给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...

  6. POJ 2195 Going Home 最小费用流 难度:1

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17955   Accepted: 9145 Descr ...

  7. POJ 2195 Going Home 最小费用流

    POJ2195 裸的最小费用流,当然也可以用KM算法解决,但是比较难写. 注意反向边的距离为正向边的相反数(因此要用SPFA) #include<iostream> #include< ...

  8. poj 3565 二分图最优匹配

    思路: 将ant与tree之间用距离来做权值,求最小权匹配就可以了.可以想到,如果有两条线段相交,那么将这两个线段交换一个顶点,使其不相交,其权值和一定会更小. 就像斜边永远比直角边长一样的道理. # ...

  9. 网络流——二分图最优匹配KM算法

    前言 其实这个东西只是为了把网络流的内容凑齐而写的(反正我是没有看到过这样子的题不知道田忌赛马算不算) 算法过程 我们令左边的点(其实二分图没有什么左右)为女生,右边的点为男生,那么: 为每一个女生定 ...

随机推荐

  1. jQuery将json字符串显示在页面上

    js代码: function syntaxHighlight(json) { if (typeof json != 'string') { json = JSON.stringify(json, un ...

  2. 合理设置apache的连接数及进程工作方式

    网站在线人数增多,访问时很慢.初步认为是服务器资源不足了,但经反复测试,一旦连接上,不断点击同一个页面上不同的链接,都能迅速打开,这种现象就是说明apache最大连接数已经满了,新的访客只能排队等待有 ...

  3. IDEA下MyBatis错误总结

    1. Pom.xml配置 语法顺序 <properties resource="config.properties"> </properties> < ...

  4. Bean with name 'xxxService' has been injected into other beans [xxxServiceA,xxxServiceB] in its raw version as part of a circular reference, but has eventually been wrapped

    启动项目,通过@Autowired注入对象,出现循环依赖,导致项目启动失败,具体报错信息如下: Exception encountered during context initialization ...

  5. B树、B+树、红黑树、AVL树

    定义及概念 B树 二叉树的深度较大,在查找时会造成I/O读写频繁,查询效率低下,所以引入了多叉树的结构,也就是B树.阶为M的B树具有以下性质: 1.根节点在不为叶子节点的情况下儿子数为 2 ~ M2. ...

  6. AS400服务程序总结

    1.服务程序的创建和调用过程 1.1生成module 1.2编写BND文件确定输出接口 1.3生成服务程序 1.3.运行调用程序时,将服务程序导入到作业内存区active group,常驻内存 2.结 ...

  7. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 忘记mysql密码

    [root@mysql-db03 ~]# mysql -uroot -poldboy123Warning: Using a password on the command line interface ...

  8. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  9. Gym - 100676G Training Camp (状压dp)

    G. Training Camp[ Color: Yellow ]Montaser is planning to train very hard for ACM JCPC 2015; he has p ...

  10. VBA Promming——入门教程

    VBA Visual Basic for Applications(VBA)是Visual Basic的一种宏语言,是微软开发出来在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言.主要能用来 ...