Spfa费用流模板
const int INF=;
const int maxn=,maxm=;
int cnt=,fir[maxn],nxt[maxm],to[maxm];
int cap[maxm],val[maxm],dis[maxn],path[maxn]; void add(int a,int b,int c,int v){
nxt[++cnt]=fir[a];to[cnt]=b;
cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
}
void addedge(int a,int b,int c,int v){
add(a,b,c,v);
add(b,a,,-v);
} int S,T;
int vis[maxn];
int Spfa(){
deque<int>q;
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
q.push_front(S);
dis[S]=;vis[S]=;
while(!q.empty()){
int x=q.front();q.pop_front();vis[x]=;
for(int i=fir[x];i;i=nxt[i])
if(cap[i]&&dis[x]+val[i]<dis[to[i]]){
dis[to[i]]=val[i]+dis[x];
path[to[i]]=i;
if(vis[to[i]])continue;
if(dis[to[i]]<dis[x])
q.push_front(to[i]);
else
q.push_back(to[i]);
vis[to[i]]=;
}
}
return dis[T]==dis[T+]?:dis[T];
} int Aug(){
int p=T,f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} int MCMF(){
int ret=,d;
while(d=Spfa())
ret+=Aug()*d;
return ret;
}
#include <queue>
using namespace std;
const int maxn=;
const int maxm=;
const int INF=;
int n,m,w[maxn],cnt,fir[maxn],to[maxm],nxt[maxm];
int cap[maxm],val[maxm],path[maxn],vis[maxn],dis[maxn];
queue<int>q;
struct Net_Flow{
Net_Flow(){cnt=;} void add(int a,int b,int c,int v){
nxt[++cnt]=fir[a];cap[cnt]=c;
to[cnt]=b;val[cnt]=v;fir[a]=cnt;
} void addedge(int a,int b,int c,int v){
add(a,b,c,v);add(b,a,,-v);
} int Spfa(int S,int T){
memset(dis,,sizeof(dis));
q.push(S);vis[S]=;dis[S]=;
while(!q.empty()){
int x=q.front();q.pop();vis[x]=;
for(int i=fir[x];i;i=nxt[i])
if(cap[i]&&dis[to[i]]>dis[x]+val[i]){
dis[to[i]]=dis[x]+val[i];
if(!vis[to[i]])q.push(to[i]);
vis[to[i]]=;path[to[i]]=i;
}
}
return dis[T];
} int Aug(int S,int T){
int p=T,f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} int MCMF(int S,int T){
int v=,d;
while((d=Spfa(S,T))!=INF)
v+=d*Aug(S,T);
return v;
}
}mcmf;
Spfa费用流模板的更多相关文章
- HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解
题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...
- HDU2686 费用流 模板
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 初识费用流 模板(spfa+slf优化) 餐巾计划问题
今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...
- hdu1533 费用流模板
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 费用流模板(带权二分图匹配)——hdu1533
/* 带权二分图匹配 用费用流求,增加源点s 和 汇点t */ #include<bits/stdc++.h> using namespace std; #define maxn 1000 ...
- 【费用流】【Next Array】费用流模板(spfa版)
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...
- POJ 2175 spfa费用流消圈
题意:给出n栋房子位置和每栋房子里面的人数,m个避难所位置和每个避难所可容纳人数.然后给出一个方案,判断该方案是否最优,如果不是求出一个更优的方案. 思路:很容易想到用最小费用流求出最优时间,在与原方 ...
- 最长可重区间集 spfa费用流
给定实直线L上的n个开区间,和一个正整数k 选取若干个区间,在保证实直线L上的任意一个点最多被选出区间覆盖k次的情况下,使得这些区间的长度和最大 先把区间按照左端点排序, 考虑到重复其实就代表着相交, ...
- ZOJ 3362 Beer Problem(SPFA费用流应用)
Beer Problem Time Limit: 2 Seconds Memory Limit: 32768 KB Everyone knows that World Finals of A ...
随机推荐
- Linux sed命令常用方法
sed也成stream editor,流编辑器,是Linux上常用的文本处理工具. 通用格式:sed 行范围 模式/RegExp/ 文件 模式: d 删除 p 打印符合条件的行 a \strin ...
- CentOS安装最新的Mysql版本
Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖命令: # yum -y remove ...
- Java使用poi对Execl简单_写_操作
public class WriteExecl { @Test public void writeExeclTest() throws Exception{ OutputStream os = new ...
- Shell - 文件运算符
文件运算符 文件运算符 描述 -b file 检测 file 是否为块设备文件 -c file 检测 file 是否为字符设备文件 -d file 检测 file 是否为目录 -e fil ...
- datazen 修改instanceid db_encryption_key
切换到Datazen.Enterprise.Server.3.0.3327.exe 所在的目录 运行如下命令: Datazen.Enterprise.Server.3.0.3327.exe DATAZ ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- jquery 操作 checkbox
对checkbox的其他几个操作 1. 全选2. 取消全选3. 选中所有奇数4. 反选5. 获得选中的所有值 js代码 $("document").ready(function() ...
- request.getSession()
request.getSession(); 与request.getSession(false);区别 服务器把session信息发送给浏览器 浏览器会将session信息存入本地cookie中 ...
- 在php中使用Memcache
在上一篇博客中我写到了memcache的安装和管理,在这一篇博客中我将写到的是在php中如何使用memcache. 一.安装memcache扩展 首先我们通过phpinfo()函数查看一下我们当前的p ...
- coreseek(sphinx)错误:WARNING: attribute 'id' not found - IGNORING
coreseek(sphinx)错误:WARNING: attribute 'id' not found - IGNORING原因及解决方法 coreseek(sphinx)建立索引时提示错误: WA ...