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. CSS--百度百科

    层叠样式表是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. CSS目前最新版本为CSS3,是能够真正做到网页表现与内容分离的一种样式设 ...

  2. [原创]JAVA号码工具类:实现手机固话号码判断与区号截取

    工具类说明 该工具类主要是用于判断号码的类型,如果是手机类型,则返回号码前7位,便于后面继续判断号码归属地:如果是固话类型,则截取固话的区号,同样也是为了后面判断号码的归属地. 在获取到这些信息之后, ...

  3. jq时间戳转化为可视化时间

    //2016年5月21日 23:12:07 function getDateTimeToDate(dt){ var dateTime = new Date(dt); var date = dateTi ...

  4. unity, 慎用DontDestroyOnLoad

    如果想让一个节点切换场景时不销毁,可以为它添加如下脚本: using UnityEngine;using System.Collections; public class dontDestroyOnL ...

  5. objective-c的观察者模式

    addObserver即添加消息响应函数.postNotificationName即发消息.

  6. 几段表单处理的JQuery代码(转)

    1 只接受数字输入 $("#uAge").keydown(function(event) { // 允许退格和删除键 if ( event.keyCode == 46 || eve ...

  7. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  8. DJI SDK iOS 开发之二:搭建主要的开发环境

    本文想介绍搭建主要的DJI SDK iOS下的开发环境,只是DJI官方已经给出了非常具体的执行其demo的教程.网址例如以下: https://dev.dji.com/cn/guide 我这里总结一下 ...

  9. 去除Android打开软件出现的红边框

    /********************************************************************** * 去除Android打开软件出现的红边框 * 说明: ...

  10. 第一百七十四节,jQuery,Ajax进阶

    jQuery,Ajax进阶 学习要点: 1.加载请求 2.错误处理 3.请求全局事件 4.JSON 和 JSONP 5.jqXHR 对象 在 Ajax 课程中,我们了解了最基本的异步处理方式.本章,我 ...