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. 使用Nginx+uWSGI+Django方法部署Django程序(下)

    在上一篇文章<五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)>中,阐述了如何只使用uWSGI来部署Django程序. 当然,单单只有uWSGI是不够的, ...

  2. Django——模版Template报错

    >>> from django.template import Template >>> t = Template("My name is {{ my_n ...

  3. SSH框架优势

    SSH框架优势 1.    典型的三层构架体现MVC(模型Model,视图View和控制)思想,可以让开发人员减轻重新建立解决复杂问题方案的负担和精力.便于敏捷开发出新的需求,降低开发时间成本. 2. ...

  4. Openerp图片路径处理

    Openerp目前存储图片如人力资源头像图片等都是以二进制的方式存储在数据库中,若要修改数据库里只存储路径可以用这种方法 Image 装饰器: Image装饰器包含3中图片显示 Image 大图片 i ...

  5. 【UVA】11468-Substring(AC自己主动机)

    AC自己主动机的题,须要注意的,建立失配边的时候,假设结点1失配边连到的那个结点2,那个结点2是一个单词的结尾,那么这个结点1也须要标记成1(由于能够看成,这个结点包括了这个单词),之后在Trie树上 ...

  6. Visual Studio C++ MFC界面常用参数更改(改变图标,添加控件,调试打印函数等等)

    背景 需要使用Visual Studio C++做一些界面.此篇文章既是记录Visual Studio C++在调整界面时常常遇见的问题. 正文 一.如何更改窗体图标,以及生成的.exe图标 更改窗体 ...

  7. 获取取并下载tuku的漫画的爬虫

    代码地址如下:http://www.demodashi.com/demo/12842.html 概述 一个简单的爬虫,实现是爬取tuku网站的漫画.并下载到脚本的文件夹中,下载的漫画按照章节名放在各自 ...

  8. 网络方面的常用命令 & 常用端口介绍

    在网络方面我们常常会用到如下命令: (1)ping命令:我们常常用来判断2台或2台以上的机器间是否网络连通. ping 192.168.1.88 -t 如果想看任何命令的参数是什么意思,我们只需要:命 ...

  9. 解决:System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用

    还原 AdventureWorks Sample DataBase 时遇到 FILESTREAM feature is disabled 错误提示. FileStream是SQL Server 200 ...

  10. 英语每日一句: What’s your point? 你究竟想说什么?

    今天我们要学习的一句话是:What's your point? 你究竟想说什么?这句话在日常交流中非经常见,当对方说了非常多东西你仍不明确他究竟是什么意思时.你就能够问What's your poin ...