Going Home POJ - 2195 (最小费用最大流)
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
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 思路
应该算是这个算法的板子题了,感觉还是只有看自己的代码才是最容易懂的。
cap表示边的容量,w表示费用。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1024;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int n,m;
char mp[108][108];
struct node
{
int x,y;
}p1[maxn],p2[maxn];
int Head[maxn],Next[maxn*maxn],v[maxn*maxn],w[maxn*maxn],cap[maxn*maxn],cnt;
int t1,t2;int ans;
void init()
{
t1=1;cnt=t2=ans=0;
memset(Head,-1,sizeof(Head));
} void add(int x,int y,int z,int f){
// cout<<x<<" "<<y<<" "<<z<<endl;
v[cnt]=y;
w[cnt]=z;
cap[cnt]=f;
Next[cnt]=Head[x];
Head[x]=cnt++; v[cnt]=x;
w[cnt]=-z;
cap[cnt]=0;
Next[cnt]=Head[y];
Head[y]=cnt++;
}
bool vis[maxn];
int dis[maxn];
int prevv[maxn],preve[maxn];
int s,t;
bool spfa()
{
queue<int>q;
memset(vis,0,sizeof(vis));
for(int i=1;i<=t;i++){
dis[i]=inf;
} dis[s]=0;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int k=Head[u];k!=-1;k=Next[k]){
if(cap[k]&&dis[v[k]]>dis[u]+w[k]){
dis[v[k]]=dis[u]+w[k];
prevv[v[k]]=u;
preve[v[k]]=k; if(!vis[v[k]]){
vis[v[k]]=true;
q.push(v[k]);
}
}
}
}
// for(int i=1;i<=t;i++){
// cout<<dis[i]<<" ";
// }
// cout<<endl;
// getchar();getchar();
if(dis[t]==inf){return false;}
else return true;
}
int min_cost_flow()
{
// fuck("???")
while(spfa()){
// cout<<"____"<<endl;
for(int i=t;i!=s;i=prevv[i]){
int k=preve[i];
cap[k]-=1;
cap[k^1]+=1;
}
// cout<<endl;
ans+=dis[t];
} } int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){
init();
for(int i=1;i<=n;i++){
scanf("%s",mp[i]+1);
for(int j=1;j<=m;j++){
if(mp[i][j]=='m'){p1[++t1]=node{i,j};}
else if(mp[i][j]=='H'){p2[++t2]=node{i,j};}
}
}
s=1;t=t1+t2+1;
for(int i=2;i<=t1;i++){
add(s,i,0,1);
for(int j=1;j<=t2;j++){
add(i,j+t1,abs(p1[i].x-p2[j].x)+abs(p1[i].y-p2[j].y),inf);
}
}
for(int i=1;i<=t2;i++){
add(i+t1,t,0,1);
} min_cost_flow();
printf("%d\n",ans);
} return 0;
}
Going Home POJ - 2195 (最小费用最大流)的更多相关文章
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
- POJ - 2195 最小费用最大流
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...
- 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个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...
随机推荐
- vue-cli: render:h => h(App)是什么意思
import Vue from 'vue' import App from './App.vue' Vue.config.productionTip = false new Vue({ render: ...
- 为什么int型最大的数是2147483647
32位的电脑中,用二进制表示,最大的就是32个1,用十进制表示为2^32-1,大概40多亿(4294967295) 对于有符号的,第一位用作表示正负(0,1),最大的就是31个1,用十进制表示为2^3 ...
- jQuery AJAX获取JSON数据解析多种方式示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 4.namespace
命名空间( namespace)是 Linux 内核的一个强大特性,为容器虚拟化的实现带来极大便 利. 利用这一特性,每个容器都可以拥有自己单独的命名空间,运行在其中的应用都像是在 独立的操作系统环境 ...
- SharePoint 2016 installation error The program can not start because api-ms-win-crt-runtime-l1-1-0.dll is missing
In this post we will discuss how we can resolve the issue The program can not start because api-ms-w ...
- 使用poi将Excel文件转换为data数据
pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...
- SQL审计 Yearning 介绍(SQL审核平台)
yearning SQL 是一个成熟的SQL审计平台
- 微信小程序——使用vue构建小程序【外传】
文档 http://mpvue.com/mpvue/ 根据文档构建完成的页面如下 更多的,还要继续看下文档~
- The Cow Lexicon POJ - 3267 dp
题意 给出一个母串 和一个字典 问母串最少删去几个字母 删去后的母串是由字典里面的单词拼起来的 思路:dp[i]表示从i到母串结尾最少需要删除多少个字母 初始化dp[length]=0 ...
- 洛谷P1209修理牛棚题解
题目 这个题一眼看上去是一个贪心,但是一个贪心题就一定要用一个贪心的做法来做吗,为什么不能写一个DP呢,这个题我们可以从他修理牛棚的角度来看,价值就是一个木板可以修几个,但是也同时消耗掉了一个木板的长 ...