POJ - 2195 最小费用最大流
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用
题解:单源点汇点最小费用最大流,每个人和房子对于建边
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct edge{
int to,Next,c;
int cost;
}e[maxn<<];
int cnt,head[N];
int s,t,ans[N][N];
int dis[N],path[N],pre[N];
void add(int u,int v,int c,int cost)
{
// cout<<u<<" "<<v<<" "<<c<<" "<<cost<<endl;
e[cnt].to=v;
e[cnt].c=c;
e[cnt].cost=cost;
e[cnt].Next=head[u];
head[u]=cnt++;
e[cnt].to=u;
e[cnt].c=;
e[cnt].cost=-cost;
e[cnt].Next=head[v];
head[v]=cnt++;
}
bool spfa()
{
memset(pre,-,sizeof pre);
memset(dis,inf,sizeof dis);
dis[s]=;
queue<int>q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=head[x];~i;i=e[i].Next)
{
int te=e[i].to;
if(e[i].c>&&dis[x]+e[i].cost<dis[te])
{
dis[te]=dis[x]+e[i].cost;
pre[te]=x;
path[te]=i;
q.push(te);
}
}
}
return pre[t]!=-;
}
int mincostmaxflow()
{
int cost=,flow=;
while(spfa())
{
int f=inf;
for(int i=t;i!=s;i=pre[i])
if(f>e[path[i]].c)
f=e[path[i]].c;
flow+=f;
cost+=dis[t]*f;
for(int i=t;i!=s;i=pre[i])
{
e[path[i]].c-=f;
e[path[i]^].c+=f;
}
}
return cost;
}
void init()
{
cnt=;
memset(head,-,sizeof head);
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m)break;
init();
int house=,man=;
pii ho[N],ma[N];
for(int i=;i<=n;++i)
{
char a[N];
scanf("%s",a+);
for(int j=;j<=m;j++)
{
if(a[j]=='H')ho[++house]=mp(i,j);
else if(a[j]=='m')ma[++man]=mp(i,j);
}
}
for(int i=;i<=house;i++)
{
for(int j=;j<=man;j++)
{
int cost=abs(ho[i].fi-ma[j].fi)+abs(ho[i].se-ma[j].se);
// cout<<i<<" "<<j<<" "<<cost<<endl;
add(i,house+j,inf,cost);
// add(house+j,i,inf,cost);
}
}
s=house+man+,t=house+man+;
for(int i=;i<=house;i++)add(s,i,,);
for(int i=;i<=man;i++)add(i+house,t,,);
printf("%d\n",mincostmaxflow());
}
return ;
}
/******************* ********************/
POJ - 2195 最小费用最大流的更多相关文章
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
- poj 3422(最小费用最大流)
题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...
- POJ 2516 最小费用最大流
每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...
- POJ 2135 最小费用最大流 入门题
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7441 Descri ...
- poj 2135最小费用最大流
最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...
- poj 3680(最小费用最大流)
题目链接:http://poj.org/problem?id=3680 思路:因为N<=200,而区间范围为[1,100000],因此需要离散化,去重,然后就是建图了相连两点连边,容量为k,费用 ...
- POJ 2315 最小费用最大流
从1走到N然后从N走回来的最短路程是多少? 转换为费用流来建模. 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E ...
- POJ 2135 最小费用最大流
题目链接 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18961 Accepted: 7326 D ...
- POJ 2195:Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...
随机推荐
- 面试10大算法汇总+常见题目解答(Java)
原文地址:http://www.lilongdream.com/2014/04/10/94.html(为转载+整理) 以下从Java的角度总结了面试常见的算法和数据结构:字符串,链表,树,图,排序,递 ...
- nodejs post请求
const http = require('http'); const querystring = require('querystring'); const postData = querystri ...
- vue_router打包(webpack)
把组件按组分块 有时候我们想把某个路由下的所有组件都打包在同个异步 chunk 中.只需要 给 chunk 命名,提供 require.ensure第三个参数作为 chunk 的名称: require ...
- key points & (QA) about RPKI
@1: Q: What does ROA look like?Since ROA means which ASes are allowed for originating routes to some ...
- socket()模块和套接字对象的内建方法
一.socket()模块函数 要使用socket.socket()函数来创建套接字,其语法如下: socket(socket_family,socket_type,protocol=0) 如上所述,s ...
- 推荐一款轻量级PHP数据库框架–Medoo
引用官网的简介: 可以加快开发速度的最轻量级的PHP数据库框架 为什么选择Medoo及其主要功能: 轻量级–单个文件,只有20KB 易用–非常容易学习和使用 功能强大–支持各种常见和复杂的SQL查询 ...
- 使用Axure生成网站结构图
使用Axure设计的各网站(产品)页面,生成网站(产品)结构图.这个对于了解网站整体结构很有帮助. 需要把它生成对应结构的网站结构图. 第一步:在“主页”上面新建一个和“主页”平级的页面,命名为“网站 ...
- Linux centos开机执行JAR Shell脚本
Linux centos开机执行shell脚本 Linux centos开机执行 java jar 1.编写jar执行脚本 vim start.sh 加入如下内容(根据自己真实路径与数据进行编写) ...
- Raspberry Pi开发之旅-远程监控
1.安装辅助工具 1 2 sudo apt-get install libjpeg8-dev sudo apt-get install cmake 2.编辑源文件 1 2 sudo git clone ...
- 把Android原生的View渲染到OpenGL Texture
http://blog.csdn.net/u010949962/article/details/41865777 最近要把Android 原生的View渲染到OpenGL GLSurfaceView中 ...