poj-2195(最小费用流)
题意:给你一个n*m的地图,H代表这个点有一个房子,m代表这个点是一个人,每次h走一步就花费一,问最小花费使得每个人能进入一个房间
代码:建立一个源点和汇点,每个人和源点相连,每个房子和汇点相连,每个人和每个房子相连,花费为曼哈段距离
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=1005000;
const int inf=0x3f3f3f3f;
struct node
{
int x,y;
}h[50050],man[50050];
int cntm,cnth;
struct Edge
{
int next;
int to;
int w;
int cost;
}edge[maxn];
int head[50050],dist[50050],pre[50050],path[50050];
int Start,End;
char s[1050][1050];
int cnt,x,y,w,n,m,tx,ty;
void add(int u,int v,int w,int cost)
{
// cout<<u<<" "<<v<<" "<<w<<" "<<cost<<endl;
edge[cnt].next=head[u];edge[cnt].to=v;
edge[cnt].w=w;edge[cnt].cost=cost;head[u]=cnt++;
//建回边
edge[cnt].next=head[v];edge[cnt].to=u;
edge[cnt].w=0;edge[cnt].cost=-cost;head[v]=cnt++;
}
bool spfa(int s,int t)
{
memset(pre,-1,sizeof(pre));
memset(dist,inf,sizeof(dist));
dist[s]=0;
queue<int>q;
q.push(s);
while(!q.empty())//不能有环,建图的时候也要注意
{
int u=q.front();q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].w>0&&dist[u]+edge[i].cost<dist[v])//这条路径存在且能被优化
{
dist[v]=dist[u]+edge[i].cost;
pre[v]=u;path[v]=i;q.push(v);
}
}
}
if(pre[t]==-1)
return false;
return true;
}
int mincost(int s,int t)
{
int cost=0;int flow=0;
while(spfa(s,t))
{
int tempflow=inf;
for(int u=t;u!=s;u=pre[u])//找最小的流量
{
if(edge[path[u]].w<tempflow)
tempflow=edge[path[u]].w;
}
flow+=tempflow;//每增广一次能得到的流量;
//cost+=dist[t]*tempflow;//花费
cost+=dist[t];
for(int u=t;u!=s;u=pre[u])
{
edge[path[u]].w-=tempflow;
edge[path[u]^1].w+=tempflow;
}
}
return cost;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
memset(head,-1,sizeof(head));
cnt=cnth=cntm=0;
Start=0;End=n*m+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{ cin>>s[i][j];
if(s[i][j]=='m')
{
man[++cntm].x=i;man[cntm].y=j;
}
if(s[i][j]=='H')
{
h[++cnth].x=i;h[cnth].y=j;
}
}
for(int i=1;i<=cnth;i++)
{
y=End;x=(h[i].x-1)*m+h[i].y;
add(x,y,1,0);
}
for(int i=1;i<=cntm;i++)
{
x=Start;y=(man[i].x-1)*m+man[i].y;
add(x,y,1,0);
}
for(int i=1;i<=cntm;i++)
{ x=man[i].x;y=man[i].y;
for(int j=1;j<=cnth;j++)
{
tx=h[j].x;ty=h[j].y;
w=abs(x-tx)+abs(y-ty);
add((x-1)*m+y,(tx-1)*m+ty,1,w);
}
}
int ans=mincost(Start,End);
cout<<ans<<endl;
}
}
poj-2195(最小费用流)的更多相关文章
- 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),最小费用流,SPFA
题目链接:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2195 Going Home 最小费用流 裸题
给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...
- POJ 2195 Going Home 最小费用流 难度:1
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17955 Accepted: 9145 Descr ...
随机推荐
- 小tips:node起一个简单服务,打开本地项目或文件浏览
1.安装nodejs 2.在项目文件夹目录下创建一个js文件,命名server.js(自定义名称),内容如下 var http = require('http'); var fs = require( ...
- Chrome opacity非1时border-radius圆角边框剪裁问题
border-radius:50%可以让元素正方形元素表现为正圆. 如果元素设置了border边框,则会表现为一个正圆圈圈,类似这样: 但有时候,border边框的这个圈圈会在边缘处发生剪裁,个别浏览 ...
- 基础环境系列:PHP7.3.0并连接pache/IIS和MySQL
版本: php7.3.0 MySQL8.0.12 Apache2.4 IIS8 一.下载PHP 1.下载php3.7 PHP版本:php7.3(7.3.0) 下载地址:https://windows ...
- <jsp:useBean>、<jsp:setProperty>与<jsp:getProperty>
<jsp:useBean>标签 会调用java对象的无参构造方法,来创建实例. <jsp:useBean>标签是用来搭配JavaBean元件的标准标签,这里指的JavaBean ...
- 如何用git上传代码到github详细步骤
注册账户 这个小菜鸟带着心跳写的第一篇博客! 还请大家多多提点! 想使用github,第一步肯定是要注册github账号,有了账号就是直接登录啦 可以直接打开http://github.com页面注册 ...
- Python XML解析之DOM
DOM说明: DOM:Document Object Model API DOM是一种跨语言的XML解析机制,DOM把整个XML文件或字符串在内存中解析为树型结构方便访问. https://docs. ...
- 【原】Java学习笔记012 - 数组
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:小店对自己的 ...
- C语言运行库翻译
这是从Visual C++ 6里面的C语言部分翻译过来. http://files.cnblogs.com/files/sishenzaixian/C运行库.zip
- A Deep Learning-Based System for Vulnerability Detection(二)
接着上一篇,这篇研究实验和结果. A.用于评估漏洞检测系统的指标 TP:为正确检测到漏洞的样本数量 FP:为检测到虚假漏洞样本的数量(误报) FN:为未检真实漏洞的样本数量(漏报) TN:未检测到漏洞 ...
- hashCode()方法对HashMap的性能影响
HashMap的put()方法会比较key的hash值,key的hash值获取方式如下: //HashMap的put方法 public V put(K key, V value) { return p ...