poj 2195 KM算法
题目链接:http://poj.org/problem?id=2195
KM算法模板~
代码如下:
#include "stdio.h"
#include "string.h"
#include "queue"
#include "math.h"
using namespace std; #define N 210
#define INF 0x3fffffff struct node{
int u,v,w,k;
int next;
}edge[4*N*N]; struct point{
int x,y;
}people[N],house[N]; bool mark[N];
int start,end;
int n,ans,idx;
int dis[N],route[N],head[N]; void init();
bool SPFA();
void EK();
void adde(int u,int v,int w,int k);
void addedge(int u,int v,int w,int k); int main()
{
int L,D;
int i,j;
int x,y,w;
char map[105][105];
while(scanf("%d%d",&L,&D),L&&D)
{
for(i=1;i<=L;i++)
scanf("%s",map[i]+1);
x=y=0;
for(i=1;i<=L;i++)
{
for(j=1;j<=D;j++)
{
if(map[i][j]=='m'){ people[x].x = i; people[x].y = j; x++;}
if(map[i][j]=='H'){ house[y].x = i; house[y].y = j; y++;}
}
}
init();
n=x;
start = 0; //起点
end = n+n+1; //终点
for(i=1;i<=n;i++)
adde(start,i,0,1); //1~n每个点代表人
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++) //n+1~n+n每个点代表house
{
w = abs(people[i-1].x-house[j-1].x)+abs(people[i-1].y-house[j-1].y);
adde(i,j+n,w,1);
}
}
for(i=1;i<=n;i++)
adde(i+n,end,0,1);
while(SPFA())
EK();
printf("%d\n",ans);
}
return 0;
} void init()
{
ans = 0;
idx = 0;
memset(head,-1,sizeof(head));
} void adde(int u,int v,int w,int k)
{
addedge(u,v,w,k);
addedge(v,u,-w,0);
} void addedge(int u,int v,int w,int k)
{
edge[idx].u = u;
edge[idx].v = v;
edge[idx].w = w;
edge[idx].k = k;
edge[idx].next = head[u];
head[u] = idx;
idx++;
} bool SPFA()
{
int i;
int x,y;
memset(route,-1,sizeof(route));
memset(mark,false,sizeof(mark));
for(i=0;i<N;i++) dis[i] = INF;
dis[0] = 0;
queue<int> q;
q.push(start);
mark[start] = true;
while(!q.empty())
{
x = q.front();
q.pop();
mark[x] = false;
for(i=head[x];i!=-1;i=edge[i].next)
{
y = edge[i].v;
if(edge[i].k && dis[y]>dis[x]+edge[i].w)
{
route[y] = i;
dis[y] = dis[x] + edge[i].w;
if(mark[y]==false)
{
mark[y] = true;
q.push(y);
}
}
}
}
route[0] = -1;
if(route[end]==-1) return false;
return true;
} void EK()
{
int x,y;
y = route[end];
while(y!=-1)
{
x = y^1;
ans+=edge[y].w;
edge[y].k--;
edge[x].k++;
y = route[edge[y].u];
}
}
poj 2195 KM算法的更多相关文章
- poj 2195(KM求最小权匹配)
题目链接:http://poj.org/problem?id=2195 思路:我们都知道KM使用来求最大权匹配的,但如果要求最小权匹配,只需把图中的权值改为负值,求一次KM,然后权值和取反即可. ht ...
- poj 2195 Going Home (km算法)
题目链接: http://poj.org/problem?id=2195 解题思路: 把man和home都提取出来,然后算出每个man和home的距离算出来,然后建立匹配图,套用km算法的模板,求最小 ...
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ - 2195 Going Home 【KM】
题目链接 http://poj.org/problem?id=2195 题意 在一张N * M 的地图上 有 K 个人 和 K 个房子 地图上每个点都是认为可行走的 求 将每个人都分配到不同的房子 求 ...
- [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4158 Accepted: 1777 Descr ...
- poj 3565 uva 1411 Ants KM算法求最小权
由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...
- (网络流 匹配 KM) Going Home --poj -- 2195
链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#problem/D 有n个人有n栋房子,每栋房子里能进一个人,但每走一格 ...
- poj - 3686 The Windy's (KM算法)
题意:n个订单和m个生产车间,每个订单在不同的车间生产所需要的时间不一样,并且每个订单只能在同一个车间中完成,直到这个车间完成这个订单就可以生产下一个订单.现在需要求完成n个订单的平均时间最少是多少. ...
- KM poj 2195
题意:给出一个地图,地图上有人和房子,问如何分配哪个人去哪个房子,走的路最短? 这道题是个完备匹配的情况下,问怎么才能走的路最少,可以用KM来做. 只不过KM算法是用来求解最大最优值,所以我们得改一下 ...
随机推荐
- IOS 之 PJSIP 笔记(一) 编译多平台支持的静态库
好久没有写博客了,这也算是我步入新工作后的第一篇技术博文吧.在进入新公司前,早就有了技术层进入下一个迭代的准备,但很多事情是意想不到的,就像我以 C# 程序员的身份面试入职的,而今却是一个全职的 IO ...
- 【分享】深入浅出WPF全系列教程及源代码
来源:http://blog.csdn.net/yishuaijun/article/details/21345893 本来想一篇一篇复制的.但是考虑到别人已经做过了,就没有必要了吧,就给大家一个目录 ...
- cppcheck
http://sourceforge.net/projects/cppcheck/files/?source=navbar https://github.com/danmar/cppcheck htt ...
- 我的HTML笔记
HTML(Hypertext Marked Language)"超文本标记语言". 1.HTML的声明 <!DOCTYPE html> 2.HTML的基本结构 < ...
- Mysql denied for user 'odbc@localhost' || denied for user 'root@localhost'
1. Question Description: 1.1 mysql version: mysql-5.7.11-win64.zip 1.2 if you connect to the mys ...
- ahjesus linux连接阿里云ubuntu服务器并更改默认账号和密码,以及创建子账户
先确保本地Linux服务器SSH服务开启,如果没有开启直接执行指令:service sshd start 然后我们使用ssh指令进行远程登陆 ssh username@ip-address 输入pas ...
- 研究jdk关于TreeMap 红黑树算法实现
因为TreeMap的实现方式是用红黑树这种数据结构进行存储的,所以呢我主要通过分析红黑树的实现在看待TreeMap,侧重点也在于如何实现红黑树,因为网上已经有非常都的关于红黑树的实现.我也看了些,但是 ...
- QT分页控件,开源,供大家使用
下载地址:http://files.cnblogs.com/dragonsuc/qt5.rar
- Android Studio 中 FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileDebugAidl'.的问题解答
Android Studio 中 FAILURE: Build failed with an exception. * What went wrong: Execution failed for ta ...
- 快速理解JS的闭包
/**闭包:1.在函数内部改变变量值,不影响函数外全局变量(相当于JAVA中私有变量)* 2.调用闭包后,最后产生的变量值并不释放.* 3.任何人调用闭包,闭包里面的值并不 ...