赤裸裸的最大流

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; #define maxn 205
#define maxm 205
#define inf (1 << 30) struct Edge
{
int next, v, f;
} edge[maxm * ]; int n, m;
int head[maxn];
int q[maxn];
bool vis[maxn];
int cur[maxn];
int dep[maxn];
int ncount;
int path[maxn]; void addedge(int a, int b, int f)
{
edge[ncount].v = b;
edge[ncount].f = f;
edge[ncount].next = head[a];
head[a] = ncount++;
} void input()
{
ncount = ;
memset(head, -, sizeof(head));
for (int i = ; i < m; i++)
{
int a, b, f;
scanf("%d%d%d", &a, &b, &f);
a--;
b--;
addedge(a, b, f);
addedge(b, a, );
}
} void bfs(int s, int t)
{
memset(vis, , sizeof(vis));
memset(dep, -, sizeof(dep));
int front = , rear = ;
q[rear++] = s;
vis[s] = true;
dep[s] = ;
while (front != rear && !vis[t])
{
int u = q[front++];
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (!vis[v] && edge[i].f > )
{
q[rear++] = v;
vis[v] = true;
dep[v] = dep[u] + ;
}
}
}
} int dinic(int s, int t)
{
int ret = ;
while (true)
{
bfs(s, t);
if (dep[t] == -)
break;
int path_n = ;
int x = s;
memcpy(cur, head, sizeof(cur));
while (true)
{
if (x == t)
{
int mink = -, delta = inf;
for (int i = ; i < path_n; ++i)
{
if (edge[path[i]].f < delta)
{
delta = edge[path[i]].f;
mink = i;
}
}
for (int i = ; i < path_n; ++i)
{
edge[path[i]].f -= delta;
edge[path[i] ^ ].f += delta;
}
ret += delta;
path_n = mink;
if (path_n)
x = edge[path[path_n - ]].v;
else
x = s;
}
int e;
for (e = cur[x]; ~e; e = edge[e].next)
{
if (edge[e].f == )
continue;
int y = edge[e].v;
if (dep[x] + == dep[y])
break;
}
cur[x] = e;
if (~e)
{
path[path_n++] = e;
x = edge[e].v;
}
else
{
if (path_n == )
break;
dep[x] = -;
--path_n;
if (path_n)
x = edge[path[path_n - ]].v;
else
x = s;
}
}
}
return ret;
} int main()
{
//freopen("t.txt", "r", stdin);
while (~scanf("%d%d", &m, &n))
{
input();
printf("%d\n", dinic(, n - ));
}
return ;
}

poj1273的更多相关文章

  1. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  2. 网络流模板(模板题:POJ1273)

    模板题:POJ1273 EK: #include <queue> #include <cstdio> #include <cstring> #include < ...

  3. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...

  4. 经典的最大流题POJ1273(网络流裸题)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  5. 最大流/最小割模板(isap) POJ1273

    isap模板核心代码: //d[]为距离标号数组,d[i]表示节点i到汇点的距离 //gap[]为GAP优化数组,gap[i]表示到汇点距离为i的节点个数 int dfs(int k,int flow ...

  6. POJ1273 最大流模板

    之前自己写的,以后当一个模板的基础吧,不管是最大流,最小割,二分图匹配 下面是POJ1273的一个裸题.. 1 #include <iostream> 2 #include <cst ...

  7. 【生活没有希望】poj1273网络流大水题

    你不能把数据规模改大点吗= =我优化都不加都过了 #include <cstdio> #define INF 2147483647 int n,m,ans,x,y,z,M; ],l[],f ...

  8. ACM/ICPC 之 网络流入门-EK算法(参考模板)(POJ1273)

    基于残留网络与FF算法的改进-EK算法,核心是将一条边的单向残留容量的减少看做反向残留流量的增加. //网络流 //EK算法 //Time:16Ms Memory:348K #include<i ...

  9. poj1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68414   Accepted: 2648 ...

  10. POJ-1273 Drainage Ditches 最大流Dinic

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...

随机推荐

  1. 嵌入AppBar并且带搜索建议的搜索框(Android)

    先看结果: 相关的官方文档在这里:Creating a Search Interface Android官方提供了两种方式: 弹出一个Dialog,覆盖当前的Activity界面 在AppBar中扩展 ...

  2. 安全相关论文--Security and Dependability

    安全相关论文--Security and Dependability 所参考的文献来自于Kreutz D, Ramos F M V, Esteves Verissimo P, et al. Softw ...

  3. final发布评论Ⅱ

    奋斗吧兄弟:奋斗吧兄弟团队提供了草原.水域.田地三种环境可以进行选择.功能如下:添加自定义生物功能可以增加生物种类,但是无法设置捕食关系,这里希望想办法完善.生物除了图片方式以外,还可以以文字的方式展 ...

  4. kubectl 命令记录 转帖自: https://www.kubernetes.org.cn/doc-45

    kubectl annotate – 更新资源的注解. kubectl api-versions – 以“组/版本”的格式输出服务端支持的API版本. kubectl apply – 通过文件名或控制 ...

  5. 【BZOJ4888】[TJOI2017]异或和(树状数组)

    [BZOJ4888][TJOI2017]异或和(树状数组) 题面 BZOJ 洛谷 题解 考虑每个位置上的答案,分类讨论这一位是否存在一,值域树状数组维护即可. #include<iostream ...

  6. Python Socket函数及说明

  7. 【洛谷P1522】牛的旅行

    题目大意:给定一个 N 个顶点的无向图,图中有若干联通块,现定义联通块的直径为该联通块中距离最远的两个点的距离,定义无向图的直径为这个图中所有联通块直径的最大值.现在在图上加一条边,使得两个本不连通的 ...

  8. PASCAL VOC数据集分析(转)

    PASCAL VOC数据集分析 PASCAL VOC为图像识别和分类提供了一整套标准化的优秀的数据集,从2005年到2012年每年都会举行一场图像识别challenge. 本文主要分析PASCAL V ...

  9. arcgis创建渔网

    创建渔网 1.     ArcToolbox > Data Management Tools > Feature Class > Create Finshnet.选择输出要素位置,模 ...

  10. Python 文件解压缩

    shutil对压缩包的处理是通过调用zipFile和tarFile两个模块来进行的. import zipfile # zipfile压缩 z = zipfile.ZipFile('ss.zip', ...