最小费用最大流 HDU1533
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1533
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=1e6+;
typedef long long LL;
typedef unsigned long long ULL;
const LL mod=1e9+;
const ULL base=1e7+;
using namespace std;
struct edge{
int to,nxt,flow,cost;
}g[maxn];
int head[],pre[],dis[],cnt,s,t,tot;
int a[][];
bool vis[];
char ss[];
void add(int x,int y,int cost,int flow){
g[cnt].to=y;
g[cnt].nxt=head[x];
g[cnt].cost=cost;
g[cnt].flow=flow;
head[x]=cnt++;
}
bool spfa(){
memset(pre,-,sizeof(pre));
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
queue<int> que;
while(!que.empty()){que.pop();}
que.push(s);
dis[s]=;
vis[s]=;
while(!que.empty()){
int k=que.front();
que.pop();
vis[k]=;
int tt=head[k];
while(tt!=-){
if(dis[g[tt].to]>dis[k]+g[tt].cost&&g[tt].flow>){
pre[g[tt].to]=tt;
dis[g[tt].to]=dis[k]+g[tt].cost;
if(vis[g[tt].to]==){
que.push(g[tt].to);
vis[g[tt].to]=;
}
}
tt=g[tt].nxt;
}
}
return pre[t]!=-;
}
int maxflow(){
int ans=;
while(spfa()){
int f=INF;
for(int i=pre[t];i!=-;i=pre[g[i^].to]){
if(g[i].flow<f)
f=g[i].flow;
}
for(int i=pre[t];i!=-;i=pre[g[i^].to]){
g[i].flow-=f;
g[i^].flow+=f;
ans+=g[i].cost*f;
}
}
return ans;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF&&n&&m){
memset(head,-,sizeof(head));
cnt=tot=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
a[i][j]=tot++;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(j!=m){
add(a[i][j],a[i][j+],,INF);
add(a[i][j+],a[i][j],-,);
add(a[i][j+],a[i][j],,INF);
add(a[i][j],a[i][j+],-,);
}
if(i!=n){
add(a[i][j],a[i+][j],,INF);
add(a[i+][j],a[i][j],-,);
add(a[i+][j],a[i][j],,INF);
add(a[i][j],a[i+][j],-,);
}
}
}
s=tot++;
t=tot++;
for(int i=;i<=n;i++){
scanf("%s",ss);
for(int j=;j<m;j++){
if(ss[j]=='H'){
add(a[i][j+],t,,);
add(t,a[i][j+],,);
}
if(ss[j]=='m'){
add(s,a[i][j+],,);
add(a[i][j+],s,,);
}
}
}
printf("%d\n",maxflow());
}
}
最小费用最大流 HDU1533的更多相关文章
- hdu1533 Going Home 最小费用最大流 构造源点和汇点
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- [hdu1533]二分图最大权匹配 || 最小费用最大流
题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...
- [板子]最小费用最大流(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的弱化版本吧..建图的关键在于: 把行.列看 ...
随机推荐
- How Region Split works in Hbase
A region is decided to be split when store file size goes above hbase.hregion.max.filesize or accord ...
- Java多线程入门中几个常用的方法
一.currentThread()方法 currentThread方法就是返回当前被调用的线程. 该方法为一个本地方法,原码如下: /** * Returns a reference to the c ...
- N!分解质因子p的个数_快速求组合数C(n,m)
int f(int n,int p) { ) ; return f(n/p,p) + n/p; } https://www.xuebuyuan.com/2867209.html 求组合数C(n,m)( ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- mysql与mysqli的区别
博客搬家了,欢迎大家关注,https://bobjin.com mysqli连接是永久连接,而MySQL是非永久连接. mysql连接:每当第二次使用的时候,都会重新打开一个新的进程. mysqli连 ...
- 关于windows下的文件结束符
在<c++ primer>中有说,在windows中文件结束符为:ctrl+z,在Linux中为:ctrl+D. 但是在while(cin>>s)的语句运行中,需要两次的^Z, ...
- java栈的实现复习
栈是一种线性表,仅限在一端进行插入和删除操作,特点是先进后出. 由于栈是一种线性结构,首先可以想到用数组来实现,但由于数组初始化后容量就已经确定,如果不添加扩容操作,则会出现栈溢出,同时扩容操作也会降 ...
- c#泛型TryParse类型转换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- DDR3初识
DDR3初识 选择2:1 ratio 意味用户总线宽度为DDR物理数据接口宽度的4倍.
- Win10下JDK环境变量的设置
1.找到jdk正确的安装路径 2.打开环境变量设置 打开"资管管理器"后,右击"此电脑",点击"属性" 然后点击"高级系统设置&q ...