最大流Dinic(模板)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=;
const int M=5e5+; class DINIC
{
public:
// const int MAXN=10004,MAXWAY=100005;
int n,way,max_flow,deep[N];
int tot,head[N],cur[N];
struct EDGE{
int to,next;
int dis;
}edge[M];
void Init(int n_)
{
tot=-;//因为加反向边要^1,所以要从0开始;
n=n_;
max_flow=;
for(int i=;i<=n_;++i)
head[i]=-;
}
void add(int from,int to,int V)
{
//正向
++tot;
edge[tot].to=to;
edge[tot].dis=V;
edge[tot].next=head[from];
head[from]=tot;
//反向
swap(from,to);
++tot;
edge[tot].to=to;
edge[tot].dis=;
edge[tot].next=head[from];
head[from]=tot;
}
queue<int>q;
bool bfs(int s,int t)
{
for(int i=;i<=n;++i)
deep[i]=inf;
while(!q.empty())q.pop();
for(int i=;i<=n;++i)cur[i]=head[i];
deep[s]=;
q.push(s); while(!q.empty())
{
int now=q.front();q.pop();
for(int i=head[now];i!=-;i=edge[i].next)
{
if(deep[edge[i].to]==inf&&edge[i].dis)
{
deep[edge[i].to]=deep[now]+;
q.push(edge[i].to);
}
}
}
return deep[t]<inf;
}
int dfs(int now,int t,int limit)
{
if(!limit||now==t)return limit;
int flow=,f;
for(int i=cur[now];i!=-;i=edge[i].next)
{
cur[now]=i;
if(deep[edge[i].to]==deep[now]+&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis))))
{
flow+=f;
limit-=f;
edge[i].dis-=f;
edge[i^].dis+=f;
if(!limit)break;
}
}
return flow;
}
void Dinic(int s,int t)
{
while(bfs(s,t))
max_flow+=dfs(s,t,inf);
}
}G;
int a[N][N],color[N][N]; int main()
{
int n,m;
while(~sc("%d%d",&n,&m))
{
ll TOT=;
for(int i=;i<=n;++i)
for(int j=;j<=m;++j)
sc("%d",&a[i][j]),TOT+=a[i][j];
G.Init(n*m+);
int S=n*m+,T=n*m+,cnt=;
for(int i=;i<=n;++i)
{
for(int j=;j<=m;++j)
{
if(i&)
{
if(j&)
color[i][j]=;
else
color[i][j]=;
}
else
{
if(j&)
color[i][j]=;
else
color[i][j]=;
}
}
}
for(int i=;i<=n;++i)
{
for(int j=;j<=m;++j)
{
++cnt;
if(color[i][j]==)
{
G.add(S,cnt,a[i][j]);
if(i>)G.add(cnt,cnt-m,inf);
if(j>)G.add(cnt,cnt-,inf);
if(i<n)G.add(cnt,cnt+m,inf);
if(j<m)G.add(cnt,cnt+,inf);
}
else
G.add(cnt,T,a[i][j]);
}
}
/* fo(i,1,n)
{
fo(j,1,m)
pr("%d ",color[i][j]);
pr("\n");
}*/
G.Dinic(S,T);
pr("%lld\n",TOT-G.max_flow);
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
最大流Dinic(模板)的更多相关文章
- 网络流--最大流dinic模板
标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...
- 最大流dinic模板
循环版,点的编号从0开始: ; ; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow; }edge[MAXM]; in ...
- 网络流-最大流 Dinic模板
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #defin ...
- hdu-4289 最大流Dinic模板题
拆点,套模板. 详情见代码. // // main.cpp // hdu_4289 // // Created by Luke on 16/8/29. // Copyright © 2016年 Luk ...
- POJ 1273 Drainage Ditches(最大流Dinic 模板)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...
- 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)
//非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...
- 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- 网络最大流dinic模板
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...
- 最大流算法 ISAP 模板 和 Dinic模板
ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...
- 洛谷P3376【模板】网络最大流 Dinic模板
之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...
随机推荐
- Python3操作YAML文件
数据及配置文件之争 数据及文件通常有三种类型: 配置文件型:如ini,conf,properties文件,适合存储简单变量和配置项,最多支持两层,不适合存储多层嵌套数据 表格矩阵型:如csv,exce ...
- UVALive 4976 Defense Lines ——(LIS变形)
题意:给出序列,能够从这序列中删去连续的一段,问剩下的序列中的最长的严格上升子串的长度是多少. 这题颇有点LIS的味道.因为具体做法就是维护一个单调的集合,然后xjbg一下即可.具体的见代码吧: #i ...
- Consul CAP理论纠错
Consul CAP理论纠错 随便搜索Consul.zookeeper.etcd.eureka注册中心比较相关博客文章,你都会发现千篇一律的是以下这幅对比图:但是我对Consul使用的是CA架构还是C ...
- jquery - 定义二维数组
var products = []; products.push({product_id: '1',count: 3},{product_id:'2',count: 6})
- 【C++】插值函数代码分析学习
插值函数代码分析学习,用的CImage类load图像. // Imagejoint.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #inc ...
- Android-Handler消息机制实现原理)(转)
Android-Handler消息机制实现原理 一.消息机制流程简介 在应用启动的时候,会执行程序的入口函数main(),main()里面会创建一个Looper对象,然后通过这个Looper对象开 ...
- java dom4j 解析xml使用实践
参考:https://dom4j.github.io/ http://www.cnblogs.com/liuling/archive/2013/02/05/dom4jxml.html 常用api: 1 ...
- python基础--字符串、元组
字符串方法 注:字符串和元组一样都是不可变的,以下函数不会改变原来字符串的值,可以将其赋给一个新的变量 st='a b c d,f,g' st_list=st.split()#字符串分割,参数不写,按 ...
- 免Root停用“Android键盘(AOSP)”
一.效果:隐藏手机状态栏输入法选择图标: 二.手段:使用ADB免root 停用系统默认Android键盘(AOSP),这里参考了大神的方法,在此表示感谢: 三.实现过程: 上图 下面就是按照大神的方法 ...
- centOS7下的静态Ip的配置。
centOS7下NAT的静态网卡的配置 最近在cenOS7下搭建大数据,发现centOS7配置静态ip的必要性.这篇博文就来谈谈如何VM虚拟机中配置centOS7的静态ip.如有不足,还望志同道合者纠 ...