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. JDBC配置MSSQL

    使用JDBC连接SQL SERVER 这可能是个很老套的话题,但不管怎么说还是有用的.姑且把配置方法贴出来吧.1. 确认Sql Server的的运行状态打开Sql Server配置管理器,确认Sql ...

  2. 09Oracle Database 数据表数据插入,更新,删除

    Oracle Database 数据表数据插入,更新,删除 插入数据 Insert into table_name(column) values(values); insert into studen ...

  3. Configure a proxy for your API calls with Angular CLI

    Table of contents Local development setup with Angular Issue: Dev server plus backend API Configurin ...

  4. Linux设置history命令显示行数以及时间

    Linux和unix上都提供了history命令,可以查询以前执行的命令历史记录但是,这个记录并不包含时间项目因此只能看到命令,但是不知道什么时间执行的如何让history记录时间呢? 解决方案 注意 ...

  5. 每日命令:(6)rmdir

    今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...

  6. spring boot 的使用(一)

    1.  启动spring-boot项目 mvn spring-boot:run cd target java -jar xxxx.jar xxxx代表生成的jar包

  7. pogresql基础学习笔记

    命令行工具:psql 可视化工具:pgAdmin 查看所有表: 命令行:\d sql:select * from pg_tables WHERE schemaname='public'; 查看表结构: ...

  8. pat甲级 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  9. nyoj 4 ASCII码排序(set,multiset)

    ASCII码排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符.   输入 第一行输 ...

  10. [luoguP1941] 飞扬的小鸟(DP)

    传送门 动归,用f[i][j]表示到达第I列高度为j时最少需要飞的次数,容易想到最裸的转移: f[i][j]=min(min(f[i-1][j-up[i-1]*k]+k),f[i-1][j+down[ ...