链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#problem/D

有n个人有n栋房子,每栋房子里能进一个人,但每走一格的价值是1, 所以要尽可能的少走,这一看很显然是匹配用KM算法,

但这是网络流专题的,不是太懂怎么用网络流来写

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; #define N 110
#define INF 0x3fffffff struct node{int x, y, step;}; char G[N][N];
int n, m1, dir[][]={{-,},{,},{,-},{,}};
int STEP[N][N], m[N][N], H[N][N], vis[N][N];
int num1, num2, lx[N], ly[N];
int used[N], visx[N], visy[N], s[N]; void BFS(node p)
{
node q, t;
queue<node>Q;
Q.push(p); memset(vis, , sizeof(vis));
vis[p.x][p.y] = ; while(Q.size())
{
q = Q.front(); Q.pop(); if(G[q.x][q.y]=='H')
STEP[m[p.x][p.y]][H[q.x][q.y]] = -q.step; for(int i=; i<; i++)
{
t.x = q.x + dir[i][];
t.y = q.y + dir[i][];
t.step = q.step + ; if(t.x>= && t.x<n && t.y>= && t.y<m1 && !vis[t.x][t.y])
{
Q.push(t);
vis[t.x][t.y] = ;
}
}
}
}
bool Find(int u)
{
visx[u] = ;
for(int i=; i<=num2; i++)
{
if(!visy[i] && lx[u]+ly[i]==STEP[u][i])
{
visy[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
else
s[i] = min(s[i], lx[u]+ly[i]-STEP[u][i]);
}
return false;
}
void KM()
{
memset(used, , sizeof(used));
memset(lx, , sizeof(lx));
memset(ly, , sizeof(ly)); for(int i=; i<=num1; i++)
for(int j=; j<=num2; j++)
lx[i] = max(lx[i], STEP[i][j]); for(int i=; i<=num1; i++)
{
for(int j=; j<=num2; j++)
s[j] = INF;
while()
{
memset(visx, , sizeof(visx));
memset(visy, , sizeof(visy)); if(Find(i))
break; int d = INF;
for(int j=; j<=num2; j++)
if(!visy[j])
d = min(d, s[j]); for(int j=; j<=num2; j++)
{
if(visx[j])
lx[j] -= d;
if(visy[j])
ly[j] += d;
}
}
}
int res = ; for(int i=; i<=num1; i++)
res -= STEP[used[i]][i]; printf("%d\n", res);
} int main()
{
while(scanf("%d%d", &n, &m1), n+m1)
{
int i, j;
node p; num1=, num2=;
memset(STEP, , sizeof(STEP));
for(i=; i<n; i++)
{
scanf("%s", G[i]);
for(j=; j<m1; j++)
{
if(G[i][j]=='m')
m[i][j] = ++num1;
if(G[i][j]=='H')
H[i][j] = ++num2;
}
} for(i=; i<n; i++)
for(j=; j<m1; j++)
{
if(G[i][j]=='m')
{
p.x=i, p.y=j, p.step=;
BFS(p);
}
} KM();
}
return ;
}

粘个别人的代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
using namespace std; const int MAXN = ;
const int oo = 1e9+; struct point{int x, y;}man[MAXN], house[MAXN];
struct Graph{int flow, cost;}G[MAXN][MAXN];
int NX, NY, start, End;///男人和房子的数目,源点和汇点 bool spfa(int pre[])
{
stack<int> sta;
int instack[MAXN]={}, dist[MAXN]; for(int i=; i<=End; i++)
dist[i] = oo; dist[start] = ;
sta.push(start); while(sta.size())
{
int u = sta.top();sta.pop();
instack[u] = false; for(int i=; i<=End; i++)
{
if(G[u][i].flow && dist[i] > dist[u]+G[u][i].cost)
{
dist[i] = dist[u] + G[u][i].cost;
pre[i] = u; if(instack[i] == false)
{
sta.push(i);
instack[i] = true;
}
}
}
} return dist[End] != oo;
}
int MinCost()
{
int i, pre[MAXN], cost=; while(spfa(pre) == true)
{///如果有增广路
int MinFlow = oo; for(i=End; i != start; i=pre[i])
MinFlow = min(MinFlow, G[pre[i]][i].flow);
for(i=End; i != start; i=pre[i])
{///逆向访问这条增广路上的每条边
int k = pre[i];
G[k][i].flow -= MinFlow;
G[i][k].flow += MinFlow;
cost += G[k][i].cost;
}
} return cost;
} int main()
{
int M, N; while(scanf("%d%d", &M, &N), M+N)
{
int i, j;char s[MAXN][MAXN]; memset(G, , sizeof(G));
NX = NY = ; for(i=; i<M; i++)
scanf("%s", s[i]); for(i=; i<M; i++)
for(j=; j<N; j++)
{
if(s[i][j] == 'm')
{
NX++;
man[NX].x = i;
man[NX].y = j;
}
if(s[i][j] == 'H')
{
NY++;
house[NY].x = i;
house[NY].y = j;
}
} for(i=; i<=NX; i++)
for(j=; j<=NY; j++)
{///房子的编号从NX~NX+NY
G[i][NX+j].flow = ;
G[i][NX+j].cost = fabs(man[i].x-house[j].x)+fabs(man[i].y-house[j].y);
G[NX+j][i].cost = -G[i][NX+j].cost;
} start = NX+NY+, End = start+; for(i=; i<=NX; i++)
{///把源点与人连接
G[start][i].flow = ;
G[start][i].cost = ;
}
for(i=; i<=NY; i++)
{///把房子和汇点连接
G[NX+i][End].flow = ;
G[NX+i][End].cost = ;
} printf("%d\n", MinCost());
} return ;
}

(网络流 匹配 KM) Going Home --poj -- 2195的更多相关文章

  1. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  2. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  3. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

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

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  5. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  6. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. poj 2195 Going Home(最小费最大流)

    poj 2195 Going Home Description On a grid map there are n little men and n houses. In each unit time ...

  8. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  9. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

随机推荐

  1. Nginx主动检测方案---Tengine

    方案选择大致如下: 1.用Tengine来代替Nginx,   http://tengine.taobao.org/ Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问 ...

  2. avalon做的抽奖效果

    .sweepstake { color: orange; font-size: 24px; font-weight: bold; } 先来一个简单的 <style> .sweepstake ...

  3. Python在pycharm中编程时应该注意的问题汇总

    1.缩进问题 在 pycharm 中点击 enter 自动进行了换行缩进,此时应该注意:比如 if   else  语句,后面跟着打印输出 print 的时候,一定注意是要if语句下的输出还是else ...

  4. OpenOffice Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

  5. distinct top執行順序

    select distinct top 3 from table; 先distinct后top

  6. pymongo的常用操作

    环境:pymongo3.0.3,python3 以下是我整理的一些关于pymongo的操作,网上很多是用pymongo.Connecion()去连接数据库的,但是我这里连接一直提示没有这个包,如果大家 ...

  7. Telephone interview with Youyou Tu

    "Good News for the National Holiday!" Telephone interview with Youyou Tu following the ann ...

  8. 关于空指针NULL、野指针、通用指针

    http://www.cnblogs.com/losesea/archive/2012/11/16/2772590.html 首先说一下什么是指针,只要明白了指针的含义,你就明白null的含义了.假设 ...

  9. Little-endian和Big-endian(小端数据和大端数据)

    Little和Big指的是内存地址的大小,end指的是数据的末尾. Little-endian指内存地址低的地方存数据的末尾(即低字节) Big-endian指内存地址高的地方存数据的末尾(即高字节) ...

  10. Super Star(最小球覆盖)

    Super Star http://poj.org/problem?id=2069 Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...