题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3549

题目大意:

给有向图,求1-n的最大流

解题思路:

直接套模板,注意有重边

传送门:网络流入门

 #include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int Map[maxn][maxn];
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f):u(u), v(v), c(c), f(f){}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init(int n)
{
for(int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void addedge(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if(now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if(u == t)return f;//已经到达源点,返回流量f
for(int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if(now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if(d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for(;;)
{
BFS(s);
if(level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
}
int T, cases;
int main()
{
cin >> T;
while(T--)
{
int n, k;
cin >> n >> k;
init(n);
memset(Map, , sizeof(Map));
int u, v, c;
for(int i = ; i < k; i++)
{
cin >> u >> v >> c;
Map[u][v] += c;
}
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(!Map[i][j])continue;
addedge(i, j, Map[i][j]);
}
}
cout<<"Case "<<++cases<<": ";
cout<<Maxflow(, n)<<endl;
}
return ;
}

hdu-3549 Flow Problem---最大流模板题(dinic算法模板)的更多相关文章

  1. hdu - 3549 Flow Problem (最大流模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...

  2. hdu 3549 Flow Problem (最大流)

    裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...

  3. hdu 3549 Flow Problem 最大流 Dinic

    题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...

  4. HDU 3549 Flow Problem(最大流)

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

  5. 网络流 HDU 3549 Flow Problem

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

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

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

  7. hdu 3549 Flow Problem

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

  8. HDU 3549 Flow Problem(最大流模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. #include< ...

  9. hdu 3549 Flow Problem 最大流问题 (模板题)

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

  10. hdu 3549 Flow Problem (网络最大流)

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

随机推荐

  1. Spark Runtime概述

    从Spark Runtime的角度来讲由五大核心对象:Master.Worker.Executor.Driver.CoarseGrainedExecutorBacked: Spark在做分布式集群系统 ...

  2. 关于双端队列 deque 模板 && 滑动窗口 (自出)

    嗯... deque 即为双端队列,是c++语言中STL库中提供的一个东西,其功能比队列更强大,可以从队列的头与尾进行操作... 但是它的操作与队列十分相似,详见代码1: 1 #include < ...

  3. Polycarp's Pockets(思维)

    Polycarp has nn coins, the value of the ii-th coin is aiai. Polycarp wants to distribute all the coi ...

  4. Python 起步 环境配置

    1:https://www.python.org/   首先进入这个网址,选择自己喜欢的版本 2:嘛,我以前装的是2.7,把下载好的安装一下就行  3:我的电脑Python的安装路径C:\Progra ...

  5. Django基础(1)

    昨日内容回顾: 1. socket创建服务器 2. http协议: 请求协议 请求首行 请求方式 url?a=1&b=2 协议 请求头 key:value 请求体 a=1&b=2(只有 ...

  6. Python 列表list 和 字符串str 互转

    一.列表list转字符串str 命令(python2.x):''.join(list) 命令(python2.x):''.join(str(s) for s in list) 其中,引号中是字符之间的 ...

  7. JS——jquery UI

    1. draggable() 滑动条demo: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  8. mysql 主从复制以及binlog 测试

    ###mysql查看binlog日志内容 https://blog.csdn.net/nuli888/article/details/52106910 mysql的binlog日志位置可通过show ...

  9. 转 怎样用grep命令去掉空格行和以#开头的行

    怎样用grep命令去掉空格行和以#开头的行 https://jingyan.baidu.com/article/359911f544c36b57fe030637.html

  10. Bazinga HDU - 5510 不可做的暴力

    http://acm.hdu.edu.cn/showproblem.php?pid=5510 想了很久队友叫我用ufs + kmp暴力过去了. fa[x] = y表示x是y的子串,所以只有fa[x] ...