Poj(2195),最小费用流,SPFA
题目链接:http://poj.org/problem?id=2195
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 21530 | Accepted: 10871 |
Description
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Input
Output
Sample Input
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
2
10
28
Source
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm> using namespace std; #define MAXN 205
#define INF 20000 bool vis[MAXN];
int cnt, cnt_h, cnt_m, result;
int d[MAXN], pre[MAXN], cost[MAXN][MAXN], cap[MAXN][MAXN]; struct node
{
int x, y;
} hos[MAXN], man[MAXN]; int step(int i, int j)
{
return (int)fabs((man[i].x-hos[j].x)*1.0) + fabs((man[i].y-hos[j].y)*1.0);
}
void make_map()
{
int i, j;
memset(cap, , sizeof(cap));
for (i = ; i < cnt_m; i++)
{
cap[][i+] = ;
cost[][i+] = ;
}
for (i = ; i < cnt_h; i++)
{
cap[cnt_m+i+][] = ;
cost[cnt_m+i+][] = ;
}
for (i = ; i < cnt_m; i++)
for (j = ; j < cnt_h; j++)
{
cap[i+][cnt_m+j+] = ;
cost[i+][cnt_m+j+] = step(i, j);
cost[cnt_m+j+][i+] = -cost[i+][cnt_m+j+];
}
} bool spfa()
{
int i, u;
for (i = ; i <= cnt; i++)
{
d[i] = INF;
vis[i] = false;
}
d[] = ;
queue <int> q;
q.push();
while (!q.empty())
{
u = q.front();
q.pop();
vis[u] = true;
for (i = ; i <= cnt; i++)
if (cap[u][i] && d[i] > d[u] + cost[u][i])
{
d[i] = d[u] + cost[u][i];
pre[i] = u;
if (!vis[i])
{
vis[i] = true;
q.push(i);
}
}
vis[u] = false;
}
if (d[] < INF)
return true;
return false;
} int main()
{
char c;
int i, j, n, m;
while (scanf("%d%d", &n, &m), n && m)
{
cnt_h = cnt_m = ;
for (i = ; i < n; i++)
for (j = ; j < m; j++)
{
scanf(" %c", &c);
if (c == 'H')
{
hos[cnt_h].x = i;
hos[cnt_h].y = j;
cnt_h++;
}
else if (c == 'm')
{
man[cnt_m].x = i;
man[cnt_m].y = j;
cnt_m++;
}
}
cnt = cnt_h + cnt_m + ;
make_map();
result = ;
while (spfa())
{
int i, cf;
cf = INF;
for (i = ; i != ; i = pre[i])
cf = min(cf, cap[pre[i]][i]);
for (i = ; i != ; i = pre[i])
{
cap[pre[i]][i] -= cf;
cap[i][pre[i]] += cf;
result += cost[pre[i]][i] * cf;
}
}
printf("%d\n", result);
}
return ;
}
Poj(2195),最小费用流,SPFA的更多相关文章
- POJ 2195 Going Home 最小费用最大流 尼玛,心累
D - Going Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- POJ 2195 Going Home (带权二分图匹配)
POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...
- 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 ...
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- poj 2195 Going Home(最小费用流)
题目链接:http://poj.org/problem?id=2195 题目大意是给一张网格,网格中m代表人,h代表房子,网格中的房子和人数量相等,人可以向上向下走,每走1步花费加1,每个房子只能住一 ...
- POJ 2195 Going Home 最小费用流 裸题
给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...
- POJ 2195 Going Home【最小费用流 二分图最优匹配】
题目大意:一个n*m的地图,上面有一些人man(m)和数量相等的house(H) 图上的距离为曼哈顿距离 问所有人住进一所房子(当然一个人住一间咯)距离之和最短是多少? 思路:一个人一间房,明显是二分 ...
随机推荐
- Groupon面经Prepare: Sort given a range && Summary: Bucket Sort
首先是如何sort一个只有0和1的数组,要求inplace. follow up是告诉一个range,如何在O(N)时间内sort好 两个pointer可解 package Sorting; impo ...
- UIMenuController和UIMenuItem的使用
UIMenuController的方法: 1.创建menucontroller + (UIMenuController *)sharedMenuController; 2.设置是否可见 - (void ...
- MyEclipse下如何安装和使用ibatis插件(网上的资料对于myeclipse8.5根本就是没有用的,所以我还是自己选择了装了一个eclipse,然后将插件装在了eclipse中)
(1)myeclipse→help→Myeclipse configuration center:点击sofeware选项卡,在Browes Software 下有一个输入框,点击add site按钮 ...
- extjs4.0下的日期控件的星期显示为y的解决办法
没有修改的时候的问题: 今天第一次写博客,就记录一下以前extjs4.2下运用日期组件的星期显示问题,当时找了n久,可能是extjs4.2才出来没多久,没有多少人发现这个问题或者说很少有人将Extjs ...
- 实现listview的条目点击后改变背景颜色
gv_categoryeffect_gridview.setChoiceMode(GridView.CHOICE_MODE_SINGLE);,再设置一个selector的背景选择器 getResour ...
- kafka管理器kafka-manager部署安装
运行的环境要求 Kafka 0.8.1.1+ sbt 0.13.x Java 7+ 功能 为了简化开发者和服务工程师维护Kafka集群的工作,yahoo构建了一个叫做Kafka管理器的基于Web工具, ...
- Android 发送短信与接收短信
package com.example.testsms; import android.app.Activity; import android.app.PendingIntent; import a ...
- windows 下双网卡在不同网络切换设置
首先你的机器需要有两块网卡,分别接到两台交换机上, ine rnet地址:192.168.1.8,子网掩码:255.255.255.0,网关:192.168.1.1 内部网地址:172. ...
- paper 58 :机器视觉学习笔记(1)——OpenCV配置
开始学习opencv! 1.什么是OpenCV OpenCV的全称是:Open Source Computer Vision Library.OpenCV是一个基于(开源)发行的跨平台计算机视觉库,可 ...
- My sql 日期格式化
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) select DATE_FORMAT(NOW(),'%Y-%m-%d %H ...