poj 2195 最小费用最大流模板
/*Source Code
Problem: 2195 User: HEU_daoguang
Memory: 1172K Time: 94MS
Language: G++ Result: Accepted
Source Code
*/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <math.h>
#include <string.h>
using namespace std;
#define V 6005
#define E 10010000
#define inf 999999999
int n,m;
char map[][];
int hp[V][],mp[V][]; int vis[V];
int dist[V];
int pre[V]; struct Edge{
int u,v,c,cost,next;
}edge[E];
int head[V],cnt;
void init(){
cnt=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int c,int cost){
edge[cnt].u=u;edge[cnt].v=v;edge[cnt].cost=cost;
edge[cnt].c=c;edge[cnt].next=head[u];head[u]=cnt++; edge[cnt].u=v;edge[cnt].v=u;edge[cnt].cost=-cost;
edge[cnt].c=;edge[cnt].next=head[v];head[v]=cnt++;
} bool spfa(int begin,int end){
int u,v;
queue<int> q; for(int i=;i<=end+;i++){
pre[i]=-;
vis[i]=;
dist[i]=inf;
}
vis[begin]=;
dist[begin]=;
q.push(begin); while(!q.empty()){ u=q.front();
q.pop();
vis[u]=; for(int i=head[u];i!=-;i=edge[i].next){
if(edge[i].c>){
v=edge[i].v;
if(dist[v]>dist[u]+edge[i].cost){
dist[v]=dist[u]+edge[i].cost;
pre[v]=i;
if(!vis[v]){
vis[v]=true;
q.push(v);
}
}
}
} } return dist[end]!=inf;
} int MCMF(int begin,int end){
int ans=,flow;
int flow_sum=; while(spfa(begin,end)){ flow=inf;
for(int i=pre[end];i!=-;i=pre[edge[i].u])
if(edge[i].c<flow)
flow=edge[i].c;
for(int i=pre[end];i!=-;i=pre[edge[i].u]){
edge[i].c-=flow;
edge[i^].c+=flow;
}
ans+=dist[end]*flow;
flow_sum+=flow; }
return ans;
} int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF){
if(n== && m==) break;
for(int i=;i<n;i++){
scanf("%s",map[i]);
}
int hcnt=,mcnt=;
for(int i=;i<n;i++)
for(int j=;j<m;j++){
if(map[i][j]=='H'){
hp[hcnt][]=i;
hp[hcnt][]=j;
hcnt++;
}
if(map[i][j]=='m'){
mp[mcnt][]=i;
mp[mcnt][]=j;
mcnt++;
}
}
hcnt--;
mcnt--;
init();
for(int i=;i<=hcnt;i++){
addedge(,i,,);
//addedge(i,0,1,0);
}
for(int j=;j<=mcnt;j++){
addedge(hcnt+j,hcnt+mcnt+,,);
//addedge(hcnt+mcnt+1,hcnt+j,1,0);
}
for(int i=;i<=hcnt;i++)
for(int j=;j<=mcnt;j++){
addedge(i,hcnt+j,,fabs(hp[i][]-mp[j][])+fabs(hp[i][]-mp[j][]));
//addedge(hcnt+j,i,1,fabs(hp[i][0]-mp[j][0])+fabs(hp[i][1]-mp[j][1]));
}
int res=MCMF(,hcnt+mcnt+);
printf("%d\n",res);
}
return ;
}
/*
2
.m
H.
5
HH..m
.....
.....
.....
mm..H
8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
20
..mm..H..H.H...HHH.m
m.H...H.....H......m
..H...mm.........m..
Hm.m..H.H...H..m....
mH.Hm....mH........H
m............m......
.m..H...........H..m
H.m.H.....H.......m.
...m..Hm.....m.H...H
..H...H....H......mH
..m.m.....m....mm...
..........H.......H.
...mm......m...H....
.....m..H.H......m.m
.H......mm.H.m.m.m.m
HH..........HH..HH..
...m..H........Hm...
....H.....H...mHm...
H...........m......m
....m...H.m.....m...
20
...Hm.m.HHH...Hmm...
.H........m.......H.
.......H...H.H......
....HmH.m....Hm..m..
....m..m............
H..H.........m....H.
.m.H...m...mH.m..H..
.mH..H.H......m...m.
...mH...H.......m...
..Hm..H..H......m.m.
..mH...H.m..m.H..HH.
m.m......m........m.
...mH..m.....mH.....
....m.H.H..........H
....H.......H....m.H
H.mH.......m.......H
..............m.HH.H
..H.........m.m.m...
.........mH.....mmm.
...mH.m.m.....H..m..
20
H.H.......H....m....
.....m..H......H..H.
...H..............m.
mH..mm..m...H.......
......H....mm.H.....
.mH..mm.....mH.H...H
.........HHH........
......H.H...mm......
.m..m.H...mHmm...HH.
mm..Hmm.H..m...m.H.m
H.Hm.m.m.....m......
...........m.......H
......m......H...m..
....H..........Hm...
.H..H.m....m........
...H....Hm..........
m.H.mHm.m.m...H...H.
.m..........m.......
H......H...HmHHm..H.
..H..m.m...m.H..H...
20
.m..m..Hm...........
.m..H.H...m.m.m...H.
........m..mH....H..
..H...........Hm.H.m
H..H.m........mm..m.
H.......m...........
..m..Hmmmm...m..mH..
..H.Hm...H..........
H....m.......mm.....
....m..m.....m.....m
.H.m.H...H.....H....
.m........mm..H...H.
..m.......H.mH..mm.H
.......Hm...HH....H.
...mm....HHH........
..H.m..H........m...
H.........H.........
HH.H.....m.H..Hm....
...H.m...H.Hm.....m.
.H..mH..H..H........
20
m.........m.......m.
..m.H....m....m...m.
m...H.m.....H.H.....
.....H.Hm.m...m.....
..mH...H.H.m.H...H..
H....H......m.....m.
..................H.
.m..m.Hm......m..H..
....H..H.m.....H...H
....m.H......m.H...m
....HH...H...H......
..H.....m......H.H..
mmH...mmm.....m.....
..m.......m...mmH...
......H.H..m...Hm..H
HHHm.H.m........H...
...mHm.......m....m.
.....mmH.H..H.....m.
......m..H.....m...H
..HH..m...mH......H.
20
m.HHm..HH..m.mHm....
mm..H...............
m...HH.......m.H....
..mH.m.m.......mmH..
H.m........m.......H
m.H....m....m..m...H
....m......mm.......
.m.H....m..H..m..H..
H....m......H.......
...H...........m.m.H
......H...m...H..m..
.mH..H.H.....m......
...m.....m.H...HmH.H
m.......H..H.H..mm.H
...H.........Hm.HH..
.m....H.....m.HHm...
...HHH...........m..
m............H......
.....m..mm.....m....
.....m..H..H..H....H
20
....H.............m.
.....HH..mH..Hm..H..
m...........mH....mm
..m..m.H......m....m
.H..........mHH....m
...........m..H.m...
..H...H.........mHm.
......H...........H.
H.....H.....H..m....
H.H..H..m...m..mH.m.
....H...m.H.mHmm.mHm
..mmm...H....m....H.
.........m..m.......
.m.H....Hm....m.....
.....H.......HH...mH
..H..H....m.m.....HH
.Hm.............H...
H...Hm.......H.m.m..
.....m..HH...H.....m
........mHmmH..m....
20
.....H.......H...m.m
.m..H.m.m....m......
m.H.HHH..mm.........
H...mH.mH...........
..mHm..m.m......m..m
H.HH.....m...m......
H.mH....H......H....
...mH.m.mHmH...H....
........H.....m.....
..H.......HmH...H...
......m...HH.m......
.H..m.H...H.........
...H...m..m..m...m..
.mH..HH......m...H.. ...m....H.H.H.m.....
.........H.m........
..m..H...H......H...
mmH..mH.....m.H..H..
H....mm...H.m...m.mm
......m.............
20
....mH..m...m.....m.
..m......mHm...H....
.H.....mHm....H..H..
...HH..........m.m..
..m..mm........m....
...m.....H..........
..mH..H...m.........
...H.H.....m..mH..m.
..H.........Hm.Hm..H
...........H......m.
.............H...mm.
H.m.....Hm.H.m......
....Hm..m..mm.H...m.
...H.H.H......H.Hm..
H.m............m....
..mH.m.m...m..H.m..H
HH.H....m.H..H...m..
.....H.....H...H...H
........mH.HHm.....m
.H.H.....mmH......m.
20
.mH........m.mmH..H.
m....H........H..H..
.........Hm.m.m.....
....H.H...m.........
.H....m.............
HH.....H.....H.HH...
mmmmmm.H..m..m......
.Hm.H...H.H..m.H....
.........m..m.mHmHH.
...m.m....m..H.Hm..H
...Hm....H..m....m..
...mH.....m.......m.
.H...HmmH..H.....H..
m.H...m.....mmH....H
.m.H......m...H.....
H........m..Hm......
.......m...........m
...m........m...H...
.........m...H......
HH..H..m...H......H.
*/
上面的网上大牛的代码,我看的好像和我的模板差不多,还有测试样例,我就复了一下,下面的是我的代码
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 19827 | Accepted: 10080 |
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
are one or more test cases in the input. Each case starts with a line
giving two integers N and M, where N is the number of rows of the map,
and M is the number of columns. The rest of the input will be N lines
describing the map. You may assume both N and M are between 2 and 100,
inclusive. There will be the same number of 'H's and 'm's on the map;
and there will be at most 100 houses. Input will terminate with 0 0 for
N and M.
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<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
//最小费用最大流,求最大费用只需要取相反数,结果取相反数即可。
//点的总数为 N,点的编号 0~N-1
const int MAXN = ;
const int MAXM = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init()
{
tol = ;
memset(head,-,sizeof (head));
}
void addedge (int u,int v,int cap,int cost){
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = ;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = ;
edge[tol].cost = -cost;
edge[tol].flow = ;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i = ; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i]. next)
{
int v = edge[i]. to;
if(edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i]. cost )
{
dis[v] = dis[u] + edge[i]. cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -)return false;
else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
if(Min > edge[i].cap - edge[i]. flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i]. cost * Min;
}
flow += Min;
}
return flow;
}
char map[];
struct node1{
int x, y;
}hh[];
struct node2{
int x,y;
}mm[];
int main(){
int n,m,sta;
while(scanf("%d%d",&n,&m)!=EOF){
if(n==&&m==)
break;
memset(hh,,sizeof(hh));
memset(mm,,sizeof(mm));
memset(map,,sizeof(map));
memset(pre,,sizeof(pre));
memset(dis,,sizeof(dis));
memset(vis,false,sizeof(vis));
memset(edge,,sizeof(edge));
memset(hh,,sizeof(hh));
memset(mm,,sizeof(mm));
init();
int u,v,w;
int cnth=,cntm=;
for(int i=;i<n;i++){
scanf("%s",map);
for(int j=;j<m;j++){
if(map[j]=='H'){
hh[++cnth].x=i;
hh[cnth].y=j;
}
if(map[j]=='m'){
mm[++cntm].x=i;
mm[cntm].y=j;
}
}
}
N=cntm+cnth+;
for(int i=;i<=cntm;i++){
for(int j=;j<=cnth;j++){
addedge(i,j+cntm,,abs(mm[i].x-hh[j].x)+abs(mm[i].y-hh[j].y));
addedge(j+cntm,i,,abs(mm[i].x-hh[j].x)+abs(mm[i].y-hh[j].y));
}
}
int ans1=;
for(int i=;i<=cntm;i++){
addedge(,i,,);
}
for(int j=cntm+;j<=cnth+cntm;j++){
addedge(j,cnth+cntm+,,);
} int temp=minCostMaxflow(,cnth+cntm+,ans1);
printf("%d\n",ans1); }
}
poj 2195 最小费用最大流模板的更多相关文章
- POJ - 2195 最小费用最大流
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
- HDU3376 最小费用最大流 模板2
Matrix Again Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)To ...
- 洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
- 最大流 && 最小费用最大流模板
模板从 这里 搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- 最小费用最大流模板(POJ 2135-Farm Tour)
最近正好需要用到最小费用最大流,所以网上就找了这方面的代码,动手写了写,先在博客里存一下~ 代码的题目是POJ2135-Farm Tour 需要了解算法思想的,可以参考下面一篇文章,个人觉得有最大流基 ...
- Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
随机推荐
- 数组Array和字符串String的indexOf方法,以及ES7(ES2016)中新增的Array.prototype.includes方法
前言 我们在判断某一个字符是否存在于一个字符串中或者某一个值是否存在于一个数组中时,ES7之前我们需要使用indexOf,ES7引入了新的方法includes 语法 数组:Array.inexOf(s ...
- win10 KMS激活
运行 输入以管理员权限输入CMD 如果已安装密匙先卸载,没有的话可以跳过 slmgr -upk 卸载密匙命令 输入对应版密匙以及KMS地址激活 1.键入命令:slmgr -ipk XXXXX-XXXX ...
- SAP ERP和C4C Account和Contact的双向同步
Account和Contact是C4C里唯一支持可以和ERP进行双向同步的主数据类别. C4C里创建一个Account:Mouser Electronics 在C4C里保存Account,自动同步到E ...
- HDU 5459 Jesus Is Here (递推,组合数学)
有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...
- 241个jquery插件—jquery插件大全
jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多javascript高手加入其team. jQuery是继prototype之后又一个优秀的Javascrīpt框架.其经典 ...
- CPP-基础:有关调用约定
在C语言中,假设咱们有这样的一个函数:int function(int a,int b) 调历时只有用result = function(1,2)的方法就能利用这个函数.然而,当高档语言被编译成计算机 ...
- Cenos7—安装
1. 进入安装界面 2. 选择语言 3. 进行分区 4. 设置root密码
- UI Testing in Xcode 7
参考文章: UI Testing in Xcode - WWDC 2015https://developer.apple.com/videos/play/wwdc2015-406/ Document ...
- python基本操作(四)
与用户交互 为什么交互? 计算机取代人类,解放劳动力 如何交互 print('-'*100) input('请输入你的姓名:') print(""100) Python2和Pyth ...
- python寻找模块的路径顺序
>>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3. ...