Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 17963    Accepted Submission(s): 8464

Problem Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
 
Input
The first line of input contains an integer T, denoting the number of test cases.
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)
 
Output
For each test cases, you should output the maximum flow from source 1 to sink N.
 
Sample Input
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
 
Sample Output
Case 1: 1
Case 2: 2
 
Author
HyperHexagon
 
Source
 
 
题意:n 个点 m 条有向边,给出 m 条边的 u,v ,cap 问求最大流,算是模板题
练习了Dinic算法
 # 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(最大流模板)的更多相关文章

  1. [hdu3549]Flow Problem(最大流模板题)

    解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...

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

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

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

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

  4. HDU-3549Flow Problem 最大流模板题

    传送门 这里是Ford-Fulkerson写的最大流模板 #include <iostream> #include <cstdio> #include <algorith ...

  5. hdu 3549 Flow Problem (最大流)

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

  6. hdu-3549 Flow Problem---最大流模板题(dinic算法模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...

  7. hdu 3549 Flow Problem 最大流 Dinic

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

  8. HDU3549:Flow Problem(最大流入门EK)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> ...

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

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

随机推荐

  1. (二)EasyUI 使用——常用组件

    1. EasyUI常用组件的基本用法 1.1 layout布局 <!-- 布局面板 大小自适应父容器 --> <div data-options="fit:true&quo ...

  2. Cocos2dx3.0过渡篇 globalZOrder()与localZOrder()

    这篇博客的标题本想叫“...3.0新的渲染...介绍”,最后还是拉不下这个脸.为啥?觉得自己对渲染的认识还是过于表面,谈不上理解.当然了,这并不影响这篇博客继续写下去.下面看一段3.0Release ...

  3. Sql Server2005 Synonyms

    1. 同义词(SYNONYM)是SQL Server 2005中新特性 它是一种对已有的或潜在的新对象给予的别名.可以在同一个数据库或者跨数据中中使用这个别名,这个别名替代了原有对象.可以建别名的对象 ...

  4. 字符串算法之 AC自己主动机

    近期一直在学习字符串之类的算法,感觉BF算法,尽管非常easy理解,可是easy超时,全部就想学习其它的一些字符串算法来提高一下,近期学习了一下AC自己主动机.尽管感觉有所收获,可是还是有些朦胧的感觉 ...

  5. Android - 返回上一个界面finish()方法

    返回上一个界面finish()方法 本文地址: http://blog.csdn.net/caroline_wendy Android能够使用finish()方法,实现函数返回的功能.当不是Activ ...

  6. Failed to import package with error: Couldn't decompress package

    解压unitypackage的时候出错.原因是路径中包括中文字符,更改成英文路径就可以. 參考 Error while importing package: Couldn't decompress p ...

  7. JavaScript Creating 对象

    可通过多种方法在 JavaScript 中创建你自己的对象.可以直接实例化Object 对象 (JavaScript),然后添加你自己的属性和方法.或者可以使用对象文本表示法来定义你的对象.还可使用构 ...

  8. Python内置函数之callable()

    callable()用来检测对象是否可调用的. callable()返回值为True或者False. 下面看看例子 : >>> callable() False >>&g ...

  9. Java并发编程(十一)实例封闭

    本节主题:如果一个类是线程不安全的,但是又要在多线程程序中安全地使用,你该怎么办? 大体有两种思路: 第一种:确保该对象是能由单个线程访问,也就是这个对象是被封闭在线程中的: 第二种:通过锁来对该对象 ...

  10. UVA 679 Dropping Balls 由小见大,分析思考 二叉树放小球,开关翻转,小球最终落下叶子编号。

    A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each ...