洛谷 3376

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define N 10010
#define rg register
using namespace std;
int n,m,S,T,tot,ans,last[N],d[N],q[N],cur[N];
struct edge{
int to,pre,f;
}e[];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
bool bfs(){
memset(d,-,sizeof(d));
d[S]=;
int front=,rear=; q[front]=S;
while(front<rear){
int now=q[front++];
for(rg int i=last[now],to;i;i=e[i].pre)
if(d[to=e[i].to]==-&&e[i].f) d[to]=d[now]+,q[rear++]=to;
}
return d[T]>=;
}
int dfs(int x,int a){
if(x==T||a==) return a;
int flow=,f;
for(rg int &i=cur[x],to;i;i=e[i].pre)
if(d[to=e[i].to]==d[x]+&&e[i].f&&(f=dfs(to,min(a,e[i].f)))){
e[i].f-=f; e[i^].f+=f; flow+=f; a-=f;
if(!a) break;
}
return flow;
}
int dinic(){
int ans=;
while(bfs()){
for(rg int i=;i<=n;i++) cur[i]=last[i];
ans+=dfs(S,2e9);
}
return ans;
}
int main(){
n=read(); m=read(); S=read(); T=read(); tot=;
for(rg int i=;i<=m;i++){
int u=read(),v=read();
e[++tot]=(edge){v,last[u],read()}; last[u]=tot;
e[++tot]=(edge){u,last[v],}; last[v]=tot;
}
printf("%d\n",dinic());
return ;
}

【模板】网络流-最大流 Dinic的更多相关文章

  1. [讲解]网络流最大流dinic算法

    网络流最大流算法dinic ps:本文章不适合萌新,我写这个主要是为了复习一些细节,概念介绍比较模糊,建议多刷题去理解 例题:codevs草地排水,方格取数 [抒情一下] 虽然老师说这个多半不考,但是 ...

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

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

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

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

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

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

  5. 网络流最大流——dinic算法

    前言 网络流问题是一个很深奥的问题,对应也有许多很优秀的算法.但是本文只会讲述dinic算法 最近写了好多网络流的题目,想想看还是写一篇来总结一下网络流和dinic算法以免以后自己忘了... 网络流问 ...

  6. Power Network(网络流最大流 & dinic算法 + 优化)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24019   Accepted: 12540 D ...

  7. POJ训练计划1459_Power Network(网络流最大流/Dinic)

    解题报告 这题建模实在是好建.,,好贱.., 给前向星给跪了,纯dinic的前向星居然TLE,sad.,,回头看看优化,.. 矩阵跑过了.2A,sad,,, /******************** ...

  8. (网络流 最大流 Dinic || SAP)Control -- hdu --4289

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4289 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. UESTC 1143 数据传输 网络流 最大流 Dinic

    数据传输 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Sta ...

随机推荐

  1. bzoj1597 [Usaco2008 Mar]土地购买——斜率优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1597 就是斜率优化水题... 然而WA了十几遍,正负号处理真让人心累... 还是该负就负,别 ...

  2. 微信公众平台appid和appsecret在哪

    1.appid和appsecret是微信公众平台服务号才有的,如果自己家的公众平台不是服务号,需要升级为服务号. 2.登录服务号,登录“服务”条目,“服务中心”如图. 3.点击“自定义菜单”-“开发模 ...

  3. easyui-filebox 文件上传

    参考文章:http://blog.csdn.net/fsdad/article/details/73200618 easyui论坛:http://www.jeasyui.com/forum/index ...

  4. deepin 安装 idea

    1.su root 2.sudo apt install idea 3.sudo vi /etc/hosts 最后一行添加 0.0.0.0 account.jetbrains.com 4.注册码 N7 ...

  5. thinkphp 5 常用的助手函数

    load_trait:快速导入Traits,PHP5.5以上无需调用 /**     * 快速导入Traits PHP5.5以上无需调用     * @param string    $class t ...

  6. Android 性能优化(11)网络优化( 7)Optimizing for Doze and App Standby

    Optimizing for Doze and App Standby In this document Understanding Doze Doze restrictions Adapting y ...

  7. DHTML_____window对象的事件

    <html> <head> <meta charset="utf-8"> <title>window对象事件</title&g ...

  8. 创建 pulic dblink 使不同的用户可以访问dblink

    1. system: grant create public database link to ivrsdata; 2.userdate: tnsnames.ora config db connect ...

  9. Quartz.Net学习笔记(1)-完整的例子

    一.开发环境 系统:Win10 编译器:VS2013 .Net版本:4.5 Quartz版本:2.3.3 二.涉及程序集 Common.Logging.Core.dll Common.Logging. ...

  10. hdu5122 K.Bro Sorting

    思路: 模拟. 实现: #include <iostream> #include <cstdio> using namespace std; ], n, t; int main ...