最大流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从 ...
随机推荐
- csp-s模拟109
这场考试状态是极差,也因而无畏地打下了三个乱搞.然而这场确实挺乱搞.T1状压但我没优化而选择循环展开,T2打$bitset$随机化(考场上打的有问题不是随机但也能A),T3贪心骗分.但是因为状态实在太 ...
- Truffle测试框架
Truffle测试框架 2018年06月08日 19:01:19 tianlongtc 阅读数 1000 Truffle 有一个标准的自动化测试框架,让你可以非常方便地测试您的合约.这个框架允许您 ...
- [GIT]比较不同分支的差异
比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: undefined 1.查看 dev 有,而 master 中没有的: 1.查看 de ...
- UML期末复习题——2.2:UML Activity Diagram.
第二题:活动图 重要概念: 活动图:一种有助于使工作流和业务过程可视化的图. 绘制要点: 具体方法见:http://www.cnblogs.com/xiaolongbao-lzh/p/4591953. ...
- 图像模糊C均值聚类分割代码
转自:直觉模糊C均值聚类与图像阈值分割 - liyuefeilong的专栏 - CSDN博客 https://blog.csdn.net/liyuefeilong/article/details/43 ...
- powershell命令教程
启动 powershell #字符串操作 对象操作 "hello".Length #进程操作 PS C:\> notepad PS C:\> $process=get- ...
- Build Telemetry for Distributed Services之OpenCensus:C#
OpenCensus Easily collect telemetry like metrics and distributed traces from your services OpenCensu ...
- Build Telemetry for Distributed Services之OpenTracing项目
中文文档地址:https://wu-sheng.gitbooks.io/opentracing-io/content/pages/quick-start.html 中文github地址:https:/ ...
- 防止sshd服务被暴力破解
方法有很多种,这里介绍两种. (1).配置安全的shhd设置 不允许root用户直接登录到系统,添加一个普通用户,必要时再切换到root用户. 修改默认端口号. 不允许密码登录,只能通过密钥登录系统. ...
- 【python画图】
1.热力图 import numpy as np import numpy.random import matplotlib.pyplot as plt # Generate some test da ...