#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(模板)的更多相关文章

  1. 网络流--最大流dinic模板

    标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...

  2. 最大流dinic模板

    循环版,点的编号从0开始: ; ; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow; }edge[MAXM]; in ...

  3. 网络流-最大流 Dinic模板

    #include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #defin ...

  4. hdu-4289 最大流Dinic模板题

    拆点,套模板. 详情见代码. // // main.cpp // hdu_4289 // // Created by Luke on 16/8/29. // Copyright © 2016年 Luk ...

  5. POJ 1273 Drainage Ditches(最大流Dinic 模板)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  6. 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)

    //非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...

  7. 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  8. 网络最大流dinic模板

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  9. 最大流算法 ISAP 模板 和 Dinic模板

    ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...

  10. 洛谷P3376【模板】网络最大流  Dinic模板

    之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...

随机推荐

  1. Python3.5以上Celery4.2.1启动报错:ImportError: cannot import name 'LRUCache' from 'kombu.utils.functional'

    服务器:Centos7 Python版本:Python 3.7.0 Celery版本:4.2.1 Python3.5以上,OrderDict不允许在迭代过程中进行修改,导致LRUCache.updat ...

  2. MySQL group_concat 介绍

    在做数据初始化的时候,由于需要修改满足条件的全部订单的状态,因此,想使用group_concat函数提取满足条件的所有订单id,以方便写回滚脚本.测试数据取自表test1,表结构和相关 insert ...

  3. Gym - 100199C

    Gym - 100199C 题意: 其实这么长的英文题面就是想告诉我们这个题是丢手绢. 解法: 找到与 $ N $ 互质的最大整数 $ K $ 即可.当 $ N $ 为奇数时, $ \frac{N-1 ...

  4. Tensorflow使用训练好的模型进行测试,发现计算速度越来越慢

    实验时要对多个NN模型进行对比,依次加载直到第8个模型时,发现运行速度明显变慢而且电脑开始卡顿,查看内存占用90+%. 原因:使用过的NN模型还会保存在内存,继续加载一方面使新模型加载特别特别慢,另一 ...

  5. SQL-W3School-高级:SQL INNER JOIN 关键字

    ylbtech-SQL-W3School-高级:SQL INNER JOIN 关键字 1.返回顶部 1. SQL INNER JOIN 关键字 在表中存在至少一个匹配时,INNER JOIN 关键字返 ...

  6. SVM算法总结

    svm算法通俗的理解在二维上,就是找一分割线把两类分开,问题是如下图三条颜色都可以把点和星划开,但哪条线是最优的呢,这就是我们要考虑的问题: 首先我们先假设一条直线为 W•X+b =0 为最优的分割线 ...

  7. MongoDB安装成windows 服务

    观看本文之间,请先移步至下面纠正部分 之前按照MongoDB官网提供的安装方法一直出错 http://cn.docs.mongodb.org/master/tutorial/install-mongo ...

  8. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_10-freemarker静态化测试-基于模板文件静态化

    把resource拷贝到test目录下 只保留文件夹结构和test1.ftl这个模板文件就可以了. 新建一个包 编写测试类 使用freemaker提供的方法生成静态文件 Configuration是i ...

  9. java引用如果是成员变量则引用本身不保存在栈上的汇编级调试证明

    很久很久没有更新博客了,因为发生太多太多猝不及防的事情,再加上自己本身也特别忙,这里补上一直想发的自己觉得很有意义的一次探索过程. 就是很多java开发人员都曾被误导的一个点——“如果一个变量是引用, ...

  10. IOS-App 运行installation failed原因及解决方法

    解决方法如下: Bundle Identifier不能为空