wind的网络流神模板--【秒过。。】【网络流】

在被刘汝佳接连坑了两次之后决定不再用刘汝佳的网络流了

之后又找了一些模板   发现wind 的最给力了

 1 const int maxn = 405;
 2 const int maxm = maxn * maxn;
 3 const int inf = 1000000000;
 4
 5 class Dinic {
 6 public :
 7     struct Node {
 8         int u, v, f, next;
 9     }e[maxm];
10
11     int head[maxn], p, lev[maxn], cur[maxn];
12     int que[maxm];
13
14     void init() {
15         p = 0;
16         memset(head, -1, sizeof(head));
17     }
18
19     void add(int u, int v, int f) {
20         e[p].u = u; e[p].v = v; e[p].f = f; e[p].next = head[u]; head[u] = p++;
21         e[p].u = v; e[p].v = u; e[p].f = 0; e[p].next = head[v]; head[v] = p++;
22     }
23
24     bool bfs(int s, int t) {
25         int u, v, qin = 0, qout = 0;
26         memset(lev, 0, sizeof(lev)); lev[s] = 1; que[qin++] = s;
27         while(qout != qin) {
28             u = que[qout++];
29             for(int i = head[u]; i != -1; i = e[i].next) {
30                 if(e[i].f > 0 && !lev[v = e[i].v]) {
31                     lev[v] = lev[u] + 1, que[qin++] = v;
32                     if(v == t) return 1;
33                 }
34             }
35         }
36         return 0;
37     }
38
39     int dfs(int s, int t) {
40         int i, f, qin, u, k;
41         int flow = 0;
42         while(bfs(s, t)) {
43             memcpy(cur, head, sizeof(head));
44             u = s, qin = 0;
45             while(1) {
46                 if(u == t) {
47                     for(k = 0, f = inf; k < qin; k++)
48                         if(e[que[k]].f < f) f = e[que[i=k]].f;
49                     for(k = 0; k < qin; k++)
50                         e[que[k]].f -= f, e[que[k]^1].f += f;
51                     flow += f, u = e[que[qin = i]].u;
52                 }
53                 for(i = cur[u]; cur[u] != -1; i = cur[u] = e[cur[u]].next)
54                     if(e[i].f > 0 && lev[u] + 1 == lev[e[i].v]) break;
55                 if(cur[u] != -1)
56                     que[qin++] = cur[u], u = e[cur[u]].v;
57                 else {
58                     if(qin == 0) break;
59                     lev[u] = -1, u = e[que[--qin]].u;
60                 }
61             }
62         }
63         return flow;
64     }
65 };
66
67 Dinic g;

时间: 2024-10-09 15:15:32

wind的网络流神模板--【秒过。。】【网络流】的相关文章

POJ 1273 Drainage Ditches (网络流Dinic模板)

Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage

HDU1532_Drainage Ditches(网络流/EK模板)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8599    Accepted Submission(s): 4005 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie's

网络流算法模板

通过USACO草地排水学习了一下网络流,终于写好了几个模板. 最大流 BFS求增广路径 简述:通过BFS在网络中找出一条最短增广路径并修改流量(前向弧加可改进量X,后向弧则减去X),当不存在增广路径时得出最大流,时间效率O(nm^2). { ID: qty1272 PROG: ditch LANG: PASCAL } program ditch; var c,f:array[0..200,0..200]of longint; path,u:array[0..200]of longint; n,i

Food (hdu 4292 网络流sap模板题)

Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3102    Accepted Submission(s): 1034 Problem Description You, a part-time dining service worker in your college's dining hall, are now confu

【网络流基础模板】

//网络流dinic //最大流=最小割 //基本建模 //建源汇,向每个点分别连所限制的边权,题目所给的边连inf int cnt=1; inline void insert(int u,int v,int w) { e[++cnt].to=v; e[cnt].next=head[u]; head[u]=cnt; e[cnt].f=w; } inline void insert(int u,int v,int w) { insert1(u,v,w); insert1(v,w,0); } inl

USACO草地排水-网络流dinic模板

广搜计算层次图,在层次图上深搜.标准dinic模板. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<vector> 5 #include<queue> 6 #include<cmath> 7 #include<algorithm> 8 #include<string.h> 9 #define INF 0x7fffff

网络流dinic模板

#include <iostream> #include <cstdlib> #include <stdio.h> #include <map> #include <cstring> #include <math.h> #include <stdlib.h> #define ll long long #define INF 0x3f3f3f3f #define cle(a) memset(a,0,sizeof(a)) us

网络流相关模板及结论

一.一些结论 1.最大流最小割定理(Maximum Flow, Minimum Cut Theorem):网络的最大流等于最小割 2.任意一个流都小于等于任意一个割(废话) 对于一个网络流图G=(V,E),其中有源点s和汇点t,那么下面三个条件是等价的: 1. 流f是图G的最大流 2. 残留网络Gf不存在增广路 3. 对于G的某一个割(S,T),此时f = C(S,T)   原文地址:https://www.cnblogs.com/member-re/p/10403065.html

hihoCoder 1394 : 网络流四&#183;最小路径覆盖 (网络流学习#4 记录)

题目链接:http://hihocoder.com/problemset/problem/1394 代码: #include<bits/stdc++.h> using namespace std; const int N=505*2+10,M=20005,INF=0x3f3f3f3f; int n,m; int c[N][N],pre[N]; int s,t; int bfs() { memset(pre,0,sizeof(pre)); queue<int>q; q.push(s)