题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549

题意:

给你一个有向图,问你1到n的最大流。

dinic模版 (n*n*m)

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = ; //点个数
const int M = ; //边个数
const int inf = 0x3f3f3f3f;
struct Edge {
int to, next, cap, flow;
}edge[M];
int tot, head[N];
void init() {
tot = 0;
memset(head, -, sizeof(head));
}
inline void add(int u, int v, int w, int rw = ) {
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = rw; edge[tot].flow = ;
edge[tot].next = head[v]; head[v] = tot++;
}
int Q[N];
int dep[N], cur[N], sta[N];
bool bfs(int s, int t, int n) {
int front = , tail = ;
memset(dep, -, sizeof(dep[])*(n + ));
dep[s] = ;
Q[tail++] = s;
while(front < tail) {
int u = Q[front++];
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dep[v] == -) {
dep[v] = dep[u] + ;
if(v == t)
return true;
Q[tail++] = v;
}
}
}
return false;
} int dinic(int s, int t, int n) {
int maxflow = ;
while(bfs(s, t, n)) {
for(int i = ; i < n; ++i)
cur[i] = head[i];
int u = s, tail = ;
while(cur[s] != -) {
if(u == t) {
int tp = inf;
for(int i = tail - ; i >= ; --i) {
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
}
maxflow += tp;
for(int i = tail - ; i >= ; --i) {
edge[sta[i]].flow += tp;
edge[sta[i] ^ ].flow -= tp;
if(edge[sta[i]].cap - edge[sta[i]].flow == )
tail = i;
}
u = edge[sta[tail] ^ ].to;
} else if(cur[u] != - && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + == dep[edge[cur[u]].to]) {
sta[tail++] = cur[u];
u = edge[cur[u]].to;
} else {
while(u != s && cur[u] == -) {
u = edge[sta[--tail] ^ ].to;
}
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
} int main()
{
int t, n, m, u, v, c;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %d", &n, &m);
init();
for(int i = ; i <= m; ++i) {
scanf("%d %d %d", &u, &v, &c);
add(u, v, c);
}
printf("Case %d: %d\n", ca, dinic(, n, n));
}
return ;
}

貌似一般比dinic更优一点

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e3 + ;
const int M = 1e4 + ;
const int inf = 0x3f3f3f3f;
struct Edge {
int to, next, cap, flow;
}edge[M];
int tot, head[N], gap[N], dep[N], cur[N];
void init() {
tot = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int w, int rw = ) {
edge[tot].next = head[u]; edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ; head[u] = tot++;
edge[tot].next = head[v]; edge[tot].to = u; edge[tot].cap = rw; edge[tot].flow = ; head[v] = tot++;
}
int Q[N];
void bfs(int start, int end) {
memset(dep, -, sizeof(dep));
memset(gap, , sizeof(gap));
gap[] = ;
int front = , rear = ;
dep[end] = ;
Q[rear++] = end;
while(front != rear) {
int u = Q[front++];
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(dep[v] != -)
continue;
Q[rear++] = v;
dep[v] = dep[u] + ;
gap[dep[v]]++;
}
}
}
int S[N];
int sap(int start, int end, int N) {
bfs(start, end);
memcpy(cur, head, sizeof(head));
int top = ;
int u = start;
int ans = ;
while(dep[start] < N) {
if(u == end) {
int Min = inf;
int inser;
for(int i = ; i < top; ++i) {
if(Min > edge[S[i]].cap - edge[S[i]].flow) {
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
}
for(int i = ; i < top; ++i) {
edge[S[i]].flow += Min;
edge[S[i] ^ ].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top] ^ ].to;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; ~i; i = edge[i].next) {
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + == dep[u]) {
flag = true;
cur[u] = i;
break;
}
}
if(flag) {
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for(int i = head[u]; ~i; i = edge[i].next) {
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) {
Min = dep[edge[i].to];
cur[u] = i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]])
return ans;
dep[u] = Min + ;
gap[dep[u]]++;
if(u != start)
u = edge[S[--top] ^ ].to;
}
return ans;
} int main()
{
int t, n, m, u, v, c;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %d", &n, &m);
init();
for(int i = ; i < m; ++i) {
scanf("%d %d %d", &u, &v, &c);
addedge(u, v, c);
}
printf("Case %d: %d\n", ca, sap(, n, n));
}
return ;
}

HDU 3549 Flow Problem (dinic模版 && isap模版)的更多相关文章

  1. hdu 3549 Flow Problem (Dinic)

    Flow ProblemTime Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  2. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  3. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  4. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

  5. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  6. HDU 3549 Flow Problem (最大流ISAP)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  7. 网络流--最大流--HDU 3549 Flow Problem

    题目链接 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y ...

  8. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  9. HDU 3549 Flow Problem 网络流(最大流) FF EK

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

随机推荐

  1. GIt-重置

    master分支在版本库的引用目录(.git/refs)中体现为一个引用文件.git/refs/heads/master,其内容就是分支中最新提交的提交ID. $ cat .git/refs/head ...

  2. Python 频繁请求问题: [Errno 104] Connection reset by peer

    Table of Contents 1. 记遇到的一个问题:[Errno 104] Connection reset by peer 记遇到的一个问题:[Errno 104] Connection r ...

  3. java web知识点

    java web知识点 1.Java知识点 基本数据类型,面向对象,异常,IO,NIO,集合,多线程,JVM,高级特性. 2.web知识点 JSP,Serlvet,JDBC,Http 掌握Cookie ...

  4. Careercup - Microsoft面试题 - 23123665

    2014-05-12 07:44 题目链接 原题: Given an array having unique integers, each lying within the range <x&l ...

  5. 【LoadRunner】场景执行报错“failed: WSA_IO_PENDING”

    问题:性能测试场景执行报“failed: WSA_IO_PENDING”错误 解决方法: 添加web_set_sockets_option("OVERLAPPED_SEND", & ...

  6. Python-S9——Day109-Git及Redis

    1.初识Git: 2.Git版本控制之stash和branch: 1.初识Git: 1.1 Git是什么? Git是一个用于帮助用户实现“版本控制”的软件: 1.2 Git安装: GIt官网:http ...

  7. python实现算法: 多边形游戏 数塔问题 0-1背包问题 快速排序

    去年的算法课挂了,本学期要重考,最近要在这方面下点功夫啦! 1.多边形游戏-动态规划 问题描述: 多边形游戏是一个单人玩的游戏,开始时有一个由n个顶点构成的多边形.每个顶点被赋予一个整数值, 每条边被 ...

  8. Halcon17 Linux 下载

    Halcon17 Linux 下载地址:http://www.211xun.com/download_page_10.html HALCON 17 是一套机器视觉图像处理库,由一千多个算子以及底层的数 ...

  9. DOM中的节点属性

    摘抄自:http://www.imooc.com/code/1589 nodeName 属性: 节点的名称,是只读的. 1. 元素节点的 nodeName 与标签名相同 2. 属性节点的 nodeNa ...

  10. flink原理介绍-数据流编程模型v1.4

    数据流编程模型 抽象级别 程序和数据流 并行数据流 窗口 时间 有状态操作 检查点(checkpoint)容错 批量流处理 下一步 抽象级别 flink针对 流式/批处理 应用提供了不同的抽象级别. ...