Flow Problem(最大流模板)
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 17963 Accepted Submission(s): 8464
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
# include <bits/stdc++.h>
using namespace std;
# define eps 1e-
# define INF 0x3f3f3f3f
# define pi acos(-1.0)
# define MXN
# define MXM struct Edge{
int from, to, flow, cap;
}edges[MXM*]; //有向边数 struct Dinic{
int n, m, s ,t ,idx; //点,边,源点,汇点,边数
vector<int> G[MXN]; //记录边
int vis[MXN]; //BFS用
int dis[MXN]; //层次
int cur[MXN]; //考虑到哪条弧 void Init(){
idx=;
for (int i=;i<=n;i++) G[i].clear(); //有附加点时要注意
} void Addedge(int u,int v,int c){
edges[idx++] = (Edge){u,v,,c};
edges[idx++] = (Edge){v,u,,};
G[u].push_back(idx-);
G[v].push_back(idx-);
} int BFS()
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(s);
dis[s] = , vis[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for (int i=;i<(int)G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (!vis[e.to]&&e.cap>e.flow)
{
dis[e.to]=dis[u]+;
vis[e.to]=;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if (x==t||a==) return a;
int flow = , temp;
for (int &i=cur[x];i<(int)G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if (dis[e.to] == dis[x]+ && (temp=DFS(e.to, min(a, e.cap-e.flow)))>)
{
e.flow+=temp;
edges[G[x][i]^].flow-=temp;
flow += temp;
a-=temp;
if (a==) break;
}
}
return flow;
} int MaxFlow(int s,int t)
{
this->s = s, this->t = t;
int flow=;
while(BFS()){
memset(cur,,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
}F; int main()
{
int T;
scanf("%d",&T);
for (int cas=;cas<=T;cas++)
{
scanf("%d%d",&F.n,&F.m);
F.Init();
for (int i=;i<=F.m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
F.Addedge(u,v,c);
}
printf("Case %d: %d\n",cas,F.MaxFlow(,F.n));
}
return ;
}
Flow Problem(最大流模板)的更多相关文章
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
- hdu - 3549 Flow Problem (最大流模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...
- hdu 3549 Flow Problem 最大流问题 (模板题)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU-3549Flow Problem 最大流模板题
传送门 这里是Ford-Fulkerson写的最大流模板 #include <iostream> #include <cstdio> #include <algorith ...
- hdu 3549 Flow Problem (最大流)
裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...
- hdu-3549 Flow Problem---最大流模板题(dinic算法模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...
- hdu 3549 Flow Problem 最大流 Dinic
题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...
- HDU3549:Flow Problem(最大流入门EK)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> ...
- hdu 3549 Flow Problem【最大流增广路入门模板题】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...
随机推荐
- 怎样优化cocos2d/x程序的内存使用和程序大小
再次感谢原创者:Steffen Itterheim.原创博客原文地址: http://www.learn-cocos2d.com/2012/11/optimize-memory-usage-bundl ...
- ionic关于隐藏底部tabs终极解决方案
网上看到很多都是写个指令,监听view进出对tab进行显示隐藏,试过挺多个,自己写了一个,都不是太让人满意,问题大多数为: 1.二级页面是隐藏了tab,但是进去三级视图发现tab又出来了 2.三级,四 ...
- PHP-深入理解Opcode缓存
1.什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译, ...
- 灰色预测--matlab&python实现
function SGrey X0 = input('请输入原始负荷数据:'); %输入原始数据 n = length(X0); %原始n年数据 %累加生成 X1 = zeros(1,n); for ...
- mongoDB 高级查询之取模查询$mod
http://hancang2000.i.sohu.com/blog/view/235140698.htm $mod取模运算 查询age取模10等于0的数据 db.student.find( { ...
- 控制应用程序重启,外部程序C# 实例
第一步:新建一个控制台项目,作为关闭当前应用程序的调用程序. using System; using System.Configuration; using System.Diagnostics; n ...
- CefSharp.WinForms
CefSharp.WinForms 一. 前言 银医通项目,现在另外一家医院需要上系统,所以项目需要重新搭建,由于这家医院的His系统和另外一家医院的His系统不同,界面风格也不一致,所以重新搭建, ...
- nginx跨域(转2)
当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header is present on the requested resource,需要给Nginx服 ...
- Angular2升级到Angular4
angular4终于在两天前发布了正式版本,那么怎么升级呢?其实angular2和angular4之间属于平滑过渡,并不像1和2之间颠覆性的重写代码. npm uninstall -g @angula ...
- Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle
Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle 1. 主键1 2. uniq index2 3. ...