网络流 dinic

 1 struct Edge {
 2     int v, w;
 3     int next;
 4 }edge[250*250];
 5 int head[50], tot;
 6 void addedge(int u, int v, int w) {
 7     edge[tot].v = v;
 8     edge[tot].w = w;
 9     edge[tot].next = head[u];
10     head[u] = tot++;
11 }
12 int lvl[305];
13 bool bfs(int s, int t) {
14     lvl[s] = 0;
15     queue<int> Q;
16     Q.push(s);
17     while(!Q.empty()) {
18         int tmp = Q.front();
19         Q.pop();
20         if(tmp == t) return true;
21         for(int i = head[tmp]; i+1; i = edge[i].next) {
22             int v = edge[i].v;
23             int w = edge[i].w;
24             if(!lvl[v] && w) {
25                 Q.push(v);
26                 lvl[v] = lvl[tmp] + 1;
27             }
28         }
29     }
30     return false;
31 }
32 int dfs(int now, int t, int F) {
33     if(now == t) return F;
34     int ret = 0, f;
35     for(int i = head[now]; i+1; i = edge[i].next) {
36         int v = edge[i].v;
37         int w = edge[i].w;
38         if(w && lvl[v] == lvl[now] + 1) {
39             f = dfs(v, t, min(F-ret, w));
40             edge[i].w -= f;
41             edge[i^1].w += f;
42             ret += f;
43             if(ret == F) return ret;
44         }
45     }
46     return ret;
47 }
48 int dinic(int s, int t) {
49     int ans = 0;
50     while(bfs(s, t)) ans += dfs(s, t, INF);
51     return ans;
52 }
时间: 2024-12-23 12:48:35

网络流 dinic的相关文章

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

[网络流dinic]日常翻新

之前的排版简直辣眼睛,重写一遍好了 模板题是草地排水poj1273 网络流的基础思想就是瞎基本搜 但是搜要搜得有技巧,有特色 最简单的搜,无限深搜直到终点 稍微改进一下,宽搜先标号然后按层搜 再改进一下,把某些确定不再使用的点剔除 要点在于建立反向边给自己一个反悔的机会,用^1找到反向边 #include <stdio.h> #include <queue> #include <algorithm> #include <string.h> using nam

hdu2732 Leapin&#39; Lizards (网络流dinic)

D - Leapin' Lizards Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasur

poj 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

网络流Dinic(本篇介绍最大流)

前言:看到网上Dinic和ISAP的比较,多数人认为ISAP更快,不容易爆栈.当然,也有少数人认为,在多数情况下,Dinic比较稳定.我认为Dinic的思路比ISAP更简明,所以选择了Dinc算法 介绍:Dinic算法本身,自然是解决最大流(普通最大流,最大流最小割)的算法.通过处理,也可以解决二分图的最大匹配(下文介绍),最大权闭合图. 算法介绍:介绍Dinic之前,我们先介绍一下最大流.在最大流的题目中,图被称为"网络",每条边的边权被称作"流量",有一个起点(

POJ 3469(Dual Core CPU-最小割)[Template:网络流dinic V2]

Language: Default Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 19321   Accepted: 8372 Case Time Limit: 5000MS Description As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Offi

网络流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

POJ 3281(Dining-网络流拆点)[Template:网络流dinic]

Language: Default Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9631   Accepted: 4446 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John