POJ2195

裸的最小费用流,当然也可以用KM算法解决,但是比较难写。

注意反向边的距离为正向边的相反数(因此要用SPFA)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=302,src=301,sink=300,INF=1e+8;
int n,m,man,house,x[maxn],y[maxn],cap[maxn][maxn],w[maxn][maxn];
vector<int> next[maxn]; void addedge(int a,int b)
{
next[a].push_back(b);next[b].push_back(a);
cap[a][b]=1;cap[b][a]=0;
} int ab(int xx)
{
if(xx<0)return xx*(-1);
else return xx;
}
int fa[maxn];
int len(int a,int b)
{
if((a==src)||(a==sink)||(b==src)||(b==sink))return 0;
return ab(x[b]-x[a])+ab(y[b]-y[a]);
}
bool inq[maxn];
int times[maxn],dist[maxn];
bool SPFA()
{ memset(inq,0,sizeof(inq));
for(int i=0;i<maxn;i++)
dist[i]=INF;
dist[src]=0;
queue<int>q;
q.push(src);
inq[src]=true;
while(!q.empty())
{
int now=q.front();
q.pop();
inq[now]=false;
for(int i=0;i<next[now].size();i++)
{
int np=next[now][i];
if((dist[np]>dist[now]+w[now][np])&&cap[now][np]>0)
{
dist[np]=dist[now]+w[now][np];
fa[np]=now;
if(!inq[np]){q.push(np);inq[np]=true;}
}
}
}
if( dist[sink]<INF)return true;
else return false;
} int augment() {
int u=sink,delta=INF;
while(u!=src)
{
if(cap[fa[u]][u]<delta)delta=cap[fa[u]][u];
u=fa[u];
}
u=sink;
while(u!=src)
{
cap[fa[u]][u]-=delta;
cap[u][fa[u]]+=delta;
u=fa[u];
}
return dist[sink]*delta;
} int main()
{ios::sync_with_stdio(false);
while(cin>>n>>m)
{
if(n==0&&m==0)return 0;
man=0;house=150;
memset(cap,0,sizeof(cap));memset(w,0,sizeof(w));
for(int i=0;i<maxn;i++)
while(next[i].size()>0)next[i].pop_back();
for(int i=0;i<n;i++)
{ for(int j=0;j<m;j++)
{
char c;
cin>>c;
if(c=='m')
{
x[man]=i;y[man]=j;
man++;
}
if(c=='H')
{
x[house]=i;y[house]=j;
house++;
}
}
}
for(int i=150;i<house;i++)
{
addedge(i,sink);
for(int j=0;j<man;j++)
addedge(j,i);
}
for(int j=0;j<man;j++) addedge(src,j);
int ans=0;
for(int i=0;i<man;i++)
for(int j=150;j<house;j++)
{
w[i][j]=len(i,j);
w[j][i]=w[i][j]*(-1);
}
while(SPFA())
{
ans+=augment();
}
cout<<ans<<endl;
} return 0;
}

  

,没什么别的了,很简单。

POJ 2195 Going Home 最小费用流的更多相关文章

  1. POJ 2195 Going Home 最小费用流 裸题

    给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...

  2. POJ 2195 Going Home 最小费用流 难度:1

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17955   Accepted: 9145 Descr ...

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

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

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

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

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

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

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

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

  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. 【POJ 2195】 Going Home(KM算法求最小权匹配)

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

  9. poj 2195 Going Home(最小费用流)

    题目链接:http://poj.org/problem?id=2195 题目大意是给一张网格,网格中m代表人,h代表房子,网格中的房子和人数量相等,人可以向上向下走,每走1步花费加1,每个房子只能住一 ...

随机推荐

  1. A useful logger function in C project.

    #cat log.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  2. [HNOI]2003 消防局的建立

    消防局的建立 本题地址:http://www.luogu.org/problem/show?pid=2279 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料 ...

  3. pygame 方块随机飞舞动画

    import pygame import random # default WIDTH=1280 HEIGHT=1060 FPS=60 sum=0 # set color WHITE=(255,255 ...

  4. Ubuntu安装Foxit PDF阅读器

    最近使用Ubuntu自带的PDF阅读器,发现使用体验较差,打算安装FoxitReader(可能是我习惯了Foxit和Adobe) Foxit官网 对系统平台要求如下:(支持Linux) 继续摸索了一下 ...

  5. JSP页面中的指令标识

    JSP页面中的指令标识 制作人:全心全意 指令标识主要用于设定整个JSP页面范围内都有效的相关信息,它是被服务器解释并执行的,不会产生任何内容输出到网页中.也就是说,指令标识对于客户端浏览器是不可见的 ...

  6. plot3d

    plot3d plot3D a format for structured grid data that was popularized by NASA's CFD visualization Aut ...

  7. 利用tempalte.js模版引擎渲染页面,作对应的数据处理

    需要启个服务 需引入jquery.js和template.js <!DOCTYPE html> <html lang="en"> <head> ...

  8. 关于zookeeper和zkfc的一些测试

    1.停掉zookeeper集群 ****进程影响****** zkfc:报错无法连接zookeeper.ClientCnxn java.net.connectexception:拒绝连接,但不会shu ...

  9. Windows窗口创建的具体步骤

    /*实现窗口创建的六步骤:第一步:创建入口函数WinMain第二步:注册窗口类第三部:实现回调函数的功能第四步:显示窗口第五步:更新窗口第六步:消息循环*/ #include "stdafx ...

  10. Windows学习总结(9)——Windows系统常用的网络控制指令

    ping 命令式用来测试TCP/IP 网络是否畅通或者网络连接速度的命 令,其原理是根据计算机唯一标示的IP 地址,当用户给目的地址发 送一个数据包时,对方就会返回一个同样大小的数据包,根据返回的 数 ...