#include<stdio.h>
#include<string.h>
#define MAXD 1000100
#define MAXM 6000600
#define INF 1000000000
int N, M, T, e, first[MAXD], next[MAXM], u[MAXM], v[MAXM], flow[MAXM];
int q[MAXD], d[MAXD], work[MAXD], s[MAXD];
void add(int a, int b, int f)
{
u[e] = a;
v[e] = b;
flow[e] = f;
next[e] = first[a];
first[a] = e;
e ++;
}
void init()
{
int i, j, a, b, f;
T = N * M - 1;
e = 0;
for(i = 0; i <= T; i ++)
first[i] = -1;
for(i = 0; i < N; i ++)
for(j = 1; j < M; j ++)
{
scanf("%d", &f);
a = i * M + j - 1;
b = i * M + j;
add(a, b, f);
add(b, a, f);
}
for(i = 1; i < N; i ++)
for(j = 0; j < M; j ++)
{
scanf("%d", &f);
a = (i - 1) * M + j;
b = i * M + j;
add(a, b, f);
add(b, a, f);
}
for(i = 1; i < N; i ++)
for(j = 1; j < M; j ++)
{
scanf("%d", &f);
a = (i - 1) * M + j - 1;
b = i * M + j;
add(a, b, f);
add(b, a, f);
}
}
int bfs()
{
int i, j, rear;
for(i = 1; i <= T; i ++)
d[i] = -1;
d[0] = 0;
rear = 0;
q[rear ++] = 0;
for(i = 0; i < rear ; i ++)
for(j = first[q[i]]; j != -1; j = next[j])
if(flow[j] && d[v[j]] == -1)
{
d[v[j]] = d[q[i]] + 1;
if(v[j] == T)
return 1;
q[rear ++] = v[j];
}
return 0;
}
int dinic()
{
int i, j, cur, r, res = 0;
while(bfs())
{
r = 0;
cur = 0;
for(i = 0; i <= T; i ++)
work[i] = first[i];
for(;;)
{
if(cur == T)
{
int a = INF, minr = r;
for(i = 0; i < r; i ++)
if(flow[s[i]] < a)
{
a = flow[s[i]];
minr = i;
}
for(i = 0; i < r; i ++)
{
flow[s[i]] -= a;
flow[s[i] ^ 1] += a;
}
r = minr;
cur = u[s[r]];
res += a;
}
for(i = work[cur]; i != -1; i = next[i])
if(flow[i] && d[v[i]] == d[cur] + 1)
break;
work[cur] = i;
if(i != -1)
{
s[r ++] = i;
cur = v[i];
}
else
{
d[cur] = -1;
if(r == 0)
break;
r --;
cur = u[s[r]];
}
}
}
return res;
}
int main()
{
while(scanf("%d%d", &N, &M) == 2)
{
init();
int res = dinic();
printf("%d\n", res);
}
return 0;
}
相关推荐
用PyCharm+Scrapy打造你的第一个爬虫:从安装到实战(附完整代码)
本文详细介绍了如何使用PyCharm和Scrapy框架从零开始构建高效网络爬虫。通过环境配置、项目创建、核心爬虫逻辑编写到数据处理与存储的完整流程,帮助Python开发者快速掌握爬虫开发技巧,并附有实战代码示例。特别适合想要学习Scrapy框架和提升爬虫技能的开发者。
网络流的最大流入门(从普通算法到dinic优化)
网络流(network-flows)是一种类比水流的解决问题方法,与线性规划密切相关。网络流的理论和应用在不断发展。而我们今天要讲的就是网络流里的一种常见问题——最大流问题。 最大流问题(maximum flow problem),一种组合最优化问题,就是要讨论如何充分利用装置的能力,使得运输的流量最大,以取得最好的效果。求最大流的标号算法最早由福特和福克逊与与1956年提出,20世纪50年代福
北京途游游戏后端开发面经
北京途游游戏后端开发面经 我面试岗位是基于Python的后端开发工程师,主要是搞游戏的服务器开发。面试一共分了两轮,之后说是看看其他的候选人再定,感觉凉凉~~ 由于受到此次新型冠状病毒疫情的影响,本次面试采用视频面试的方式,下面是面试的时候面试官提的问题,python的基础知识还是问的比较多的。 一面:(32分钟微信视频) 简单做下自我介绍, 1.说一下你开发过的项目,挑一个说下你都做了...
Dinic Algorithm to solve max flow problem
// // main.cpp // Dinic // // Created by Longxiang Lyu on 8/12/16. // Copyright © 2016 Longxiang Lyu. All rights reserved. // // reference: // https://sites.google.com/site/indy256/algo_cpp/dinic_
LPC2124定时器实现跑马灯
文章目录一、keil 4新建工程 一、keil 4新建工程 1、Create New Project,注意,最好添加进一个新建好的文件夹里,keil不会帮你新建。 2、添加设备类型:NXP目录下的LPC2124 3、选择启动文件Startu.s 4、new 一个file,保存命名为.c文件 5、添加进project里,右击,add 选中,添加 6、设置编译输出.hex文件 ...
我装了10个ClawHub技能,这些最好用!
ClawHub就是Clawdbot的"应用商店"。Clawdbot是手机ClawHub是App Store技能就是各种App装了技能,Clawdbot才能干更多事。这个技能解决了我的信息过载问题。为什么装我订阅了很多Newsletter。每天收到一堆长文章,根本看不完。怎么用“总结这篇文章的核心观点”“把这个Newsletter提炼成3个要点”“这篇技术博客有哪些可以实践的建议?安装命令真实体验现在我收到长文章,直接转发给Clawdbot。它会提炼出核心要点和行动清单。
max_flow(Dinic)
#include #include #include #include #include using namespace std; const int INF = 0x3fffffff; int g[1005][1005]; int pre[1005]; int m; int bfs(int s,int
android 9.0Settings休眠时间项(屏幕超时)添加永不休眠功能
在9.0的系统产品开发中,系统中默认息屏时间为1分钟,可以通过在Setting中的息屏时间选项设置对应的息屏时间,但是在开发平板类的产品中,需要实现永不休眠的功能,所以需要在Settings中的息屏时间中添加永不休眠时间这一项来实现这个功能,接下来看息屏时间的相关功能,来添加这个功能
max_flow(Dinic) 分类: ACM TYPE ...
#include <cstdio> #include <iostream> #include <cstring> #include<queue> #include<cmath> using namespace std; const int INF = 0x3fffffff;...
【项目实战】AES实施应符合NIST密码算法验证程序
AES由美国国家标准与技术研究院(NIST)制定。NIST是负责开发和维护美国政府使用的技术标准的机构,包括加密标准。
【水文学法总结】河道内生态流量计算方法(含MATLAB实现代码)
简介水文学法实现河道内生态流量计算
【HDU4289】
int n,m; Max_Flow MF; int main() { freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m) != EOF) { int S,T; scanf("%d%d",&S,&T); MF.Init(2*n+2,S,T
华为 MetaERP 总账模块依托 “交易即核算” 的事件驱动架构,与 Oracle EBS 相比,更强调业务事件自动触发财务动作、实时数据同步和智能规则引擎,实现业务与财务的深度融合
华为MetaERP总账模块采用"交易即核算"架构,相比Oracle EBS实现了三大突破:1)业务事件自动触发财务动作,如采购收货自动生成暂估凭证;2)业务财务数据实时同步,支持即时分析;3)内置智能规则引擎,实现自动匹配、核销和结转。该模块覆盖采购、销售、资产、薪酬等全业务场景,支持多维核算和关联交易自动抵消,显著提升核算效率与准确性。其中采购暂估、销售结转等场景实现全自动化处理,较EBS减少80%手工操作。
Dinic 与 SAP(ISAP?) 模板
发一个最大流模板 DinicDinicDinic //vis为int类型 //sz为总点数 namespace Dinic { inline bool bfs() { int head = 0, tail = 0; vis[S] = ++cur; q[tail++] = S; dis[S] = 0; while(head &amp;amp;amp;amp;lt; tail) { ...
visio安装包
Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。
TMS_WebGMaps_1.9.8.0_Full_Source
TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder
模板:最大流
const int maxn =1000+10; struct Edge { int from,to,cap,flow; Edge(){} Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){} }; struct Dinic { int n,m,s,t; vector<Edge> edges; vector<int> G[maxn]; int cu.
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
hdu 1532 Drainage Ditches (网络流两种方法)
hdu 1532 Drainage Ditches (网络流两种方法)
HDU 3549 Flow Problem 网络最大流问题 EK、Dinic、ISAP三种算法
纯求网络最大流,用EK算法就可以实现。
power oj 2783: 上决╇ф的精确打击问题
传送门 分析:很显然,最小割模型分析:很显然,最小割模型分析:很显然,最小割模型 code:code:code: #include <map> #include <list> #include <cmath> #include <stack> #include <queue> #include <string> #include <bitset> #include <vector> #include<cs
Flow Problem 网络流dinic算法 模板题
初学网络流,有很多地方还是不太懂。 借用一下大佬的讲解:https://www.cnblogs.com/SYCstudio/p/7260613.html 题目: Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum fl...
HDU 3549 Flow Problem 最大流 最小增广路 DINIC算法 也是46MS
http://blog.youkuaiyun.com/ipqhjjybj/article/details/9172347 不说什么啦。。。这个直接是套模板的,就是前期优化了一点点,把重复边都合并了,效率提升挺明显的。 从472MS提高到了62MS,后来将 memset(a,0,sizeof(a));这样的写法改成了memset(a,0,(n+1)*sizeof(int)); 时间从 62MS 调高到了46
HDU 3549 Flow Problem - 更新dinic模板..整成结构体...
我发现我以前写的dinic并没有完全做对...效率不高...这次更新了dinic的模板..整成一个结构体..用起来方便..效率不错....不过效率没有sap高..按道理来说dinic和sap的效率没有差别的...实在不想学sap了...先这样吧.... Program: #include #include #include #include #include #include #def
[USACO4.2]完美的牛栏The Perfect Stall
题目:USACO Training 4.2(在官网上提交需加文件输入输出)、洛谷P1894。 题目大意:有n头奶牛m个牛栏,每头牛只会在自己喜欢的牛栏里产奶,问一次最多有多少奶牛能产奶。 解题思路:二分图匹配。这里我用了网络流,先建立超级源点和超级汇点,跑最大流即可。以下是Dinic算法的代码。 C++ Code: #include<cstdio> #include...
[HDU] 3549 Flow Problem [最大流][Dinic][读取优化]
Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.Input The first line of input c
HDU 3549 Flow Problem(网络流 ,FF EK Dinic)
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input contains an integer...
杭电3549 Flow Problem(最大流Dinic+前向星模板)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8276 Accepted Submission(s): 3849 Problem Description Network flow is a
[Vijos P1524]最小监视代价
题目大意:有一些点和一些边,每条边有一个监视代价。一个人从1号点出发,到达某些“传送点”可以逃生。但被监视的边就不能走了。要你用最小的监视代价使得那个人无法传送。 解题思路:求最小割。先建一个超级汇点$n+1$,把所有传送点连上这个超级汇点,然后根据“最小割=最大流”的定理,跑最大流即可。注意无向图。 以下为Dinic算法代码。 C++ Code: #include&l...
hdu3549 Flow Problem (Dinic)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 #include #include #define INF 0x3f3f3f3f #define MAX_VER 20 #define MAX_EDGE 2002 struct node { int to,cap,next; }edge[MAX_EDGE]; int he
867



被折叠的 条评论
为什么被折叠?



