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. [Mac A]如何学习Mac编程?

    http://ourcoders.com/thread/show/5550/ @tinyfool 看了您在quora上回答的『为什么说程序员是最好的职业』,加上本来就想学编程(但是一直以来因为各种借口 ...

  2. JAVA的IO流:打印流

    打印流: 打印流是输出信息最方便的类,注意包含字节打印流PrintStream和字符打印流:PrintWriter.打印流提供了非常方便的打印功能, 可以打印任何类型的数据信息,例如:小数,整数,字符 ...

  3. spring自己主动装配Bean属性

    spring提供了3种类型的自己主动装配 byName:把与Bean的属性具有同样名字(或者ID)的其它Bean自己主动装配到Bean的相应属性中. byType:把与Bean的属性具有同样类型的其它 ...

  4. scrollTop()--返回或设置匹配元素的滚动条的垂直位置

    scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置. scroll top offset 指的是滚动条相对于其顶部的偏移. 如果该方法未设置参数,则返回以像素计的相对滚动条顶部的偏移. ...

  5. jpa in查询

    List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); Map<String, Obj ...

  6. Python pip install Twisted 出错“Command "c:\python37\python.exe -u -c "import setuptools, tokenize;__file__='C:...\\Twisted\\setup.py'.... failed with error code 1 in C:... \\Twisted"

    如标题所说: python版本是目前最新的3.7.1 结果发现并不是环境问题,而是直接 pip install Twisted 安装的包不兼容 需要手动下载兼容的扩展包Twisted-18.9.0-c ...

  7. STL vector的构造函数和析构函数(2)

    原文来自:点击打开链接 译文: public member function vector的构造器:这里我仅仅翻译C++11的,C++98的就不翻译了. 构造器原型: <vector> s ...

  8. java - day14 - InnerClass

    内部类使用 package com.InnerClass; public class Mama { String name; Baby baby; Mama(String name){ this.na ...

  9. android studio中文乱码问题

    在build.gradle中加入代码: tasks.withType(JavaCompile) { options.encoding = "UTF-8" }

  10. Perl/C#连接Oracle/SQL Server和简单操作

    连接数据库是一个很常见也很必须的操作.先将我用到的总结一下. 1. Perl 连接数据库 Perl 连接数据库的思路都是: 1)使用DBI模块: 2)创建数据库连接句柄dbh: 3)利用dbh创建语句 ...