Going Home(最小费用最大流)
Going Home
http://poj.org/problem?id=2195
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 26135 | Accepted: 13106 |
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
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<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std; const int INF=0x3f3f3f3f;
const int N=;
const int M=;
int top;
int dist[N],pre[N];
bool vis[N];
int c[N];
int maxflow; struct Vertex{
int first;
}V[N];
struct Edge{
int v,next;
int cap,flow,cost;
}E[M]; void init(){
memset(V,-,sizeof(V));
top=;
maxflow=;
} void add_edge(int u,int v,int c,int cost){
E[top].v=v;
E[top].cap=c;
E[top].flow=;
E[top].cost=cost;
E[top].next=V[u].first;
V[u].first=top++;
} void add(int u,int v,int c,int cost){
add_edge(u,v,c,cost);
add_edge(v,u,,-cost);
} bool SPFA(int s,int t,int n){
int i,u,v;
queue<int>qu;
memset(vis,false,sizeof(vis));
memset(c,,sizeof(c));
memset(pre,-,sizeof(pre));
for(i=;i<=n;i++){
dist[i]=INF;
}
vis[s]=true;
c[s]++;
dist[s]=;
qu.push(s);
while(!qu.empty()){
u=qu.front();
qu.pop();
vis[u]=false;
for(i=V[u].first;~i;i=E[i].next){
v=E[i].v;
if(E[i].cap>E[i].flow&&dist[v]>dist[u]+E[i].cost){
dist[v]=dist[u]+E[i].cost;
pre[v]=i;
if(!vis[v]){
c[v]++;
qu.push(v);
vis[v]=true;
if(c[v]>n){
return false;
}
}
}
}
}
if(dist[t]==INF){
return false;
}
return true;
} int MCMF(int s,int t,int n){
int d;
int i,mincost;
mincost=;
while(SPFA(s,t,n)){
d=INF;
for(i=pre[t];~i;i=pre[E[i^].v]){
d=min(d,E[i].cap-E[i].flow);
}
maxflow+=d;
for(i=pre[t];~i;i=pre[E[i^].v]){
E[i].flow+=d;
E[i^].flow-=d;
}
mincost+=dist[t]*d;
}
return mincost;
}
string mp[];
int dir[][]={,,,,,-,-,};
int main(){
int n,m;
int v,u,w,c;
int s,t;
while(cin>>n>>m){
if(!n&&!m) break;
init();
for(int i=;i<n;i++){
cin>>mp[i];
}
s=,t=n*m+;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(mp[i][j]=='m'){
add(s,i*m+j+,,);
}
else if(mp[i][j]=='H'){
add(i*m+j+,t,,);
}
}
}
int xx,yy;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
for(int k=;k<;k++){
xx=i+dir[k][];
yy=j+dir[k][];
if(xx>=&&xx<n&&yy>=&&yy<m){
add(i*m+j+,xx*m+yy+,INF,);
}
}
}
}
int ans=MCMF(s,t,t+);
cout<<ans<<endl;
}
}
Going Home(最小费用最大流)的更多相关文章
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- bzoj1927最小费用最大流
其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→ =_=你TM逗我 刚要删突然感觉dinic的模 ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
- 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 821 Solved: 502[Submit][Status ...
- hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***
题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙, 每个逮捕队伍在每个城市可以选 ...
- UVa11082 Matrix Decompressing(最小费用最大流)
题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...
- UVa12092 Paint the Roads(最小费用最大流)
题目大概说一个n个点m条带权有向边的图,要给边染色,染色的边形成若干个回路且每个点都恰好属于其中k个回路.问最少要染多少边权和的路. 一个回路里面各个点的入度=出度=1,那么可以猜想知道各个点如果都恰 ...
- POJ3686 The Windy's(最小费用最大流)
题目大概说要用m个工厂生产n个玩具,第i个玩具在第j个工厂生产要Zij的时间,一个工厂同一时间只能生成一个玩具,问最少的用时. 这题建的图不是很直观.. 源点向玩具连容量1费用0的边 将每个工厂拆成n ...
随机推荐
- 《转》深入理解Activity启动流程(三)–Activity启动的详细流程1
本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--Activity启 ...
- Beta阶段第1周/共2周 Scrum立会报告+燃尽图 01
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2383] 版本控制:https://git.coding.net/liuyy08 ...
- python3高阶函数:map(),reduce(),filter()的区别
转载请注明出处:https://www.cnblogs.com/shapeL/p/9057152.html 1.map():遍历序列,对序列中每个元素进行操作,最终获取新的序列 print(list( ...
- 详解UML图之类图 (转)
原址: https://www.jianshu.com/p/4cd95d4ddb59 2. 怎么画类图?用什么工具? 使用工具:Visio或者processon在线作图 在类图中一共包含了以下几种模 ...
- vue music 歌单组件
在data里面定义 discList: [] methods: { _getRecommend() { getRecommend().then((res) => { if(res.code == ...
- Python ImportError: No module named Image
/********************************************************************************* * Python ImportEr ...
- HDU3861The King’s Problem
HDU3861 kosaraju缩点+最小路径覆盖 为什么是最小路径覆盖呢,我们假设有一个如下DAG图 目前我们1出发到了3处,对于3的儿子4.5.6,肯定是不能彼此到达的.所以最好的情况3只能延 ...
- [UOJ300][CTSC2017]吉夫特
uoj bzoj luogu sol 根据\(Lucas\)定理,\(\binom nm \mod 2=\binom{n\%2}{m\%2}\times\binom{n/2}{m/2}\mod 2\) ...
- ES6必知必会 (七)—— Generator 函数
Generator 函数 1.Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同,通常有两个特征: function关键字与函数名之间有一个星号: 函数体内部使 ...
- html5 data属性的使用
html5 data属性定义和用法 <ul> <li data-animal-type="bird">Owl</li> <li data- ...