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

有若干个人和若干个房子在一个给定网格中,每人走一个都要一定花费,每个房子只能容纳一人,现要求让所有人进入房子,且总花费最小。

构造一个超级源s和超级汇t,超级源s与U中所有点相连,费用cost[s][u]=0(这是显然的),容量cap[s][u]=1;V中所有点与超级汇t相连,

费用cost[v][t]=0(这是显然的),容量cap[t][v]=1。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
const int INF=<<;
const int maxn=;
struct node
{
int x,y;
}p[maxn],h[maxn];
int c[maxn][maxn],f[maxn][maxn],w[maxn][maxn];
int n,m,pn,hn,s,t,ans;
bool inq[maxn]; //是否在队列中
char str[maxn][maxn];
int pre[maxn],dis[maxn];//上一条弧,Bellman_ford; void spfa()
{
int i,v;
queue<int>q;
for (i = ; i < maxn; i++)
{
dis[i] = INF;
pre[i] = -;
inq[i] = false;
}
q.push(s);
inq[s] = true;
dis[s] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
inq[u] = false;
for (v = ; v <= t; v++)
{
if (c[u][v]&& dis[v] > dis[u] + w[u][v])
{
dis[v] = dis[u] + w[u][v];
pre[v] = u;
if (!inq[v])
{
q.push(v);
inq[v] = true;
}
}
}
}
}
void mcmf()
{
while ()
{
spfa();
if (pre[t] == -) break;
int x = t,minf = INF;
while (pre[x] != -)
{
minf = min(minf,c[pre[x]][x]);
x = pre[x];
}
x = t;
while (pre[x] != -)
{
c[pre[x]][x]-=minf;
c[x][pre[x]]+=minf;
ans+=minf*w[pre[x]][x];
x = pre[x];
}
}
} int main()
{
int i,j,pn,hn;
while(cin>>n>>m && n || m)
{
pn = hn = ;
memset(c,,sizeof(c));
memset(f,,sizeof(f));
memset(w,,sizeof(w));
for(i = ; i < n; i++)
{
scanf("%s",str[i]);
for(j = ; j < m; j++)
{
if(str[i][j] == 'H')
{
h[++hn].x = i;
h[hn].y = j;
}
else if(str[i][j] == 'm')
{
p[++pn].x = i;
p[pn].y = j;
}
}
}
s = ;
t = pn + hn + ;
for (i = ; i <= pn; i++)
c[s][i] = ;
for (i = ; i <= hn; i++)
c[i + pn][t] = ;
for (i = ; i <= pn; i++)
{
for (j = ; j <= hn; j++)
{
c[i][j + pn] = ;
w[i][j + pn] = abs(p[i].x - h[j].x) + abs(p[i].y - h[j].y);
w[j + pn][i] = -w[i][j + pn];
} }
ans = ;
mcmf();
printf("%d\n",ans);
}
return ;
}

poj 2195 Going Home(最小费用最大流)的更多相关文章

  1. POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...

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

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

  3. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  4. POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]

    ---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...

  5. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  6. POJ 3680: Intervals【最小费用最大流】

    题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...

  7. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  8. [poj] 1235 Farm Tour || 最小费用最大流

    原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...

  9. POJ 2516 Minimum Cost [最小费用最大流]

    题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...

随机推荐

  1. 如何开发 Grunt 插件

    创建 grunt 插件 准备工作:(node/npm/git 安装,在此不做赘述) yeoman generator 可以自动生成一个插件模板. 安装 yo npm install -g yo 安装 ...

  2. 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句

    这次给大家介绍一下在Java开发过程中 使用自定义注解开发:主要知识点:            1.反射            主要用于提取注解信息            2.自定义异常  主要是为了 ...

  3. DB2中循环日期跑数据

    1.数据库版本: 2.具体实现方式: ),)) /*************************************************************************** ...

  4. jsp与servlet之间的参数传递【转】

    JSP与 servlet之间的传值有两种情况:JSP -> servlet, servlet -> JSP. 通过对象 request和 session (不考虑 application) ...

  5. 计算器(delphi)

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. android 注销

    1.在个人中心退出系统MainActivity 2.清空保存的登录数据 3.打开登录LoginActivity 方法: SharedPreferencesManager.getInstance(mCo ...

  7. easy ui 菜单和按钮(Menu and Button)

    http://www.zi-han.net/case/easyui/menu&button.html

  8. C++ 字符串各种处理

    要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...

  9. 微软职位内部推荐-SDE

    微软近期Open的职位: Organization Summary:Engineering, Community & Online (ECO) is looking for a great & ...

  10. Linux计算机进程地址空间与内核装载ELF

    本文基于Linux™系统对进程创建与加载进行分析,文中实现了Linux库函数fork.exec,剖析内核态执行过程,并进一步展示进程创建过程中进程控制块字段变化信息及ELF文件加载过程. 一.初识Li ...