【模板】【网络流】Dinic
- /*
- 唐代杜荀鹤
- 《小松》
- 自小刺头深草里,而今渐觉出蓬蒿。
- 时人不识凌云木,直待凌云始道高。
- */
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #include <utility>
- #include <iomanip>
- #include <string>
- #include <cmath>
- #include <queue>
- #include <assert.h>
- #include <map>
- #include <ctime>
- #include <cstdlib>
- #include <stack>
- #include <set>
- #define LOCAL
- const int INF = 0x7fffffff;
- const int MAXN = + ;
- const int maxnode = * + * ;
- const int MAXM = + ;
- const int MAX = ;
- using namespace std;
- struct Edge{
- int u, v, c, f;
- void init(int a, int b, int d, int e){
- u = a;v = b;
- c = d;f = e;
- }
- }edges[MAXM];
- int next[MAXN], M, cur[MAXN], vis[MAXN];
- int dist[MAXN], head[MAXN], n, m, sta, end;
- void addEdge(int u, int v, int c){
- edges[M].init(u, v, c, );M++;
- edges[M].init(v, u, , );//反向边
- next[M - ] = head[u];
- head[u] = M - ;
- next[M] = head[v];
- head[v] = M++;
- return;
- }
- void init(){
- M = ;//总边数
- memset(head, -, sizeof(head));
- scanf("%d%d", &n, &m);
- for (int i = ; i <= m; i++){
- int u, v, c;
- scanf("%d%d%d", &u, &v, &c);
- addEdge(u, v, c);
- }
- sta = ;
- end = n;
- }
- bool bfs(){
- memset(vis, , sizeof(vis));
- queue<int>Q;
- dist[sta] = ;
- vis[sta] = ;
- Q.push(sta);
- while (!Q.empty()){
- int u = Q.front();
- Q.pop();
- for (int i = head[u]; i != -; i = next[i]){
- int e = i, v = edges[e].v;
- if (vis[v]) continue;
- if (edges[e].c > edges[e].f){
- vis[v] = ;
- dist[v] = dist[u] + ;
- Q.push(v);
- }
- }
- }
- return vis[end];
- }
- int dfs(int x, int a){
- if (x == end || a == ) return a;
- int flow = , f;
- if (cur[x] == -) cur[x] = head[x];
- for (int &i = cur[x]; i != -; i = next[i]){
- int e = i, v = edges[i].v;
- if (dist[v] == dist[x] + && (f = dfs(v, min(edges[e].c - edges[e].f, a))) > ){
- flow += f;
- a -= f;
- edges[e].f += f;
- edges[e ^ ].f -= f;
- if (a == ) break;
- }
- }
- return flow;
- }
- int Dinic(){
- int flow = ;
- while (bfs()){
- memset(cur, -, sizeof(cur));
- flow += dfs(sta, INF);
- }
- return flow;
- }
- int main(){
- #ifdef LOCAL
- freopen("data.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- #endif
- init();
- printf("%d", Dinic());
- //printf("%d", (a == c));
- return ;
- }
【模板】【网络流】Dinic的更多相关文章
- 模板——网络流Dinic
感谢这位大佬的博客:https://www.cnblogs.com/SYCstudio/p/7260613.html 给予了我莫大的帮助! 主要说一下网络流的几个注意点: 1.和二分图匹配相似,无法继 ...
- 模板-网络流-Dinic
//Dinic struct Edge{ int from,to,cap,flow; Edge(){ } Edge(int a,int b,int c,int d){ from=a; to=b; ca ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- POJ 3281 [网络流dinic算法模板]
题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...
- 网络流dinic ek模板 poj1273
这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...
- Power Network POJ - 1459 网络流 DInic 模板
#include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...
- 网络流Dinic模板
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
随机推荐
- HDOJ/HDU 2547 无剑无我(两点间的距离)
Problem Description 北宋末年,奸臣当道,宦官掌权,外侮日亟,辽军再犯.时下战火连连,烽烟四起,哀鸿遍野,民不聊生,又有众多能人异士群起而反,天下志士云集响应,景粮影从. 值此危急存 ...
- HDU 3045 Picnic Cows(斜率优化DP)
Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- iOS不越狱装收费App——注册iOS设备为开发者工具
额,这篇教程主要是我写下来用于总结注册iOS设备和用iResign安装App的过程,想要不越狱安装App当然有办法,但是有几个前提--你是一个Apple开发者,或者你有个朋友是App的开发者.如果没有 ...
- HDU-1300(基础方程DP-遍历之前所有状态)
Problem Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, p ...
- 如何将下载好的jar包添加到maven本地仓库
1.首先在pom.xml中添加 <dependency> <groupId>org.springframework.security</groupId> ...
- hud1856 并查集
Problem Description Mr Wang wants some boys to help him with a project. Because the project is rathe ...
- 回某位朋友问题备受phpcgi.exe煎熬现在cpu跑满(解决方案)
(本文原创,但是cgi参数参考一个大神写的针对小服务器的,希望大家积极投票哦) 下面是对php-cgi.ext过多引起服务器cup%的解决方法,希望对大家有帮助;大多数情况是发生在第四项上; 解决 ...
- 找出Active Directory架构操作主机方法!
- js 解析 bytearray 成 字符串
function bin2String(array) { return String.fromCharCode.apply(String, array); } var bit=[104,101,108 ...
- spin_lock & mutex_lock的差别?
本文由该问题引入到内核锁的讨论,归纳例如以下 为什么须要内核锁? 多核处理器下,会存在多个进程处于内核态的情况,而在内核态下,进程是能够訪问全部内核数据的,因此要对共享数据进行保护,即相互排斥处理 有 ...