Flow Problem

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

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
 

题解:最大流入门题目,就是个模版,就是不要知道为啥,要是+=。。。

代码:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
int N;
queue<int>dl;
int vis[MAXN],pre[MAXN];
int map[MAXN][MAXN];
int s,e;
bool bfs(){
while(!dl.empty())dl.pop();
mem(vis,);
mem(pre,);
vis[s]=;
dl.push(s);
int a;
while(!dl.empty()){
a=dl.front();
dl.pop();
if(a==e)return true;
for(int i=;i<=N;i++){
if(!vis[i]&&map[a][i])dl.push(i),vis[i]=,pre[i]=a;
}
}
return false;
}
int maxflow(){
int ans=;
while(){
if(!bfs())return ans;
int a=e,temp=INF;
while(a!=s){
temp=min(temp,map[pre[a]][a]);
a=pre[a];
}a=e;
while(a!=s){
map[pre[a]][a]-=temp;
map[a][pre[a]]+=temp;
a=pre[a];
}
ans+=temp;
}
}
int main(){
int T,M,flot=;
scanf("%d",&T);
while(T--){
mem(map,);
scanf("%d%d",&N,&M);
int a,b,c;
while(M--){
scanf("%d%d%d",&a,&b,&c);
map[a][b]+=c;//加等是因为在两个点可能存在多条管道,需要合并容量。。。 }
s=;e=N;
printf("Case %d: %d\n",++flot,maxflow());
}
return ;
}

dinic算法:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=;
int edgnum;
int vis[MAXN],dis[MAXN];
int head[MAXM];
queue<int>dl;
int s,e,tflow;
struct Node{
int from,to,next,flow,cup;
}dt[MAXM];
void initial(){
mem(head,-);
edgnum=;
}
void add(int u,int v,int w){
Node E={u,v,head[u],,w};
dt[edgnum]=E;
head[u]=edgnum++;
E={v,u,head[v],,};
dt[edgnum]=E;
head[v]=edgnum++;
}
bool bfs(){
mem(vis,);mem(dis,-);
dis[s]=;
while(!dl.empty())dl.pop();
dl.push(s);
vis[s]=;
int a;
while(!dl.empty()){
a=dl.front();
dl.pop();
for(int i=head[a];i!=-;i=dt[i].next){
Node b=dt[i];
if(!vis[b.to]&&b.cup>b.flow){
dis[b.to]=dis[a]+;
vis[b.to]=;
if(b.to==e)return true;
dl.push(b.to);
}
}
}
return false;
}
/************/
int dfs(int x,int a)//把找到的这个路径上所有的边的当前流量都增加a(a是所找出路径的边中 残余流量的最小值)
{
if(x==e||a==)
return a;
int flow=,f;
for(int i=head[x];i!=-;i=dt[i].next)//从上次考虑的弧开始
{
Node &E=dt[i];
if(dis[E.to]==dis[x]+&&(f=dfs(E.to,min(a,E.cup-E.flow)))>)//可继续增广
{
E.flow+=f;//正向边
dt[i^].flow-=f;//反向边
flow+=f;//总流量 加上 f
a-=f;//最小可增流量 减去f
if(a==)
break;
}
}
return flow;}
/***************/
/*void dfs(int x,int a){
for(int i=head[x];i!=-1;i=dt[i].next){
Node &b=dt[i];
if(dis[b.to]==dis[x]+1){
if(b.cup-b.flow>0&&b.to!=e){
tflow=min(tflow,b.cup-b.flow);
dfs(b.to,min(a,b.cup-b.flow));
b.flow+=tflow;
dt[i^1].flow-=tflow;
a-=tflow;
if(!a)break;
}
}
}
}*/
int maxflow(){
int flow=;
while(bfs()){
//tflow=INF;
flow+=dfs(s,INF);
//flow+=tflow;
}
return flow;
}
int main(){
int T,N,M,flot=;
scanf("%d",&T);
while(T--){
initial();
scanf("%d%d",&N,&M);
int u,v,w;
while(M--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
s=;e=N;
printf("Case %d: %d\n",++flot,maxflow());
}
return ;
}

Flow Problem(最大流)的更多相关文章

  1. Flow Problem(最大流模板)

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

  2. hdu 3549 Flow Problem (最大流)

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

  3. hdu 3549 Flow Problem 最大流 Dinic

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

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

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

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

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

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

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

  7. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

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

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

  9. HDU 3549 Flow Problem (最大流ISAP)

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

  10. hdu------(3549)Flow Problem(最大流(水体))

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

随机推荐

  1. MYSQL++之Connect类型

    原文转自:www.cnblogs.com/aicro mysqlpp:: Connect类型主要负责连接事宜,这是在所有开始mysql操作之前必须进行的(这是句废话). 该类型的主要的结果如下所示 m ...

  2. c 统计字符串中字符出现的个数

    1.单纯用数组来解题 思路:从左往右循环,每次碰到一个字符就和左边的字符串比较,如果有相同的就右移,如果没有找到相同的就从这个位置向右统计个数并输出. #include<stdio.h> ...

  3. docNet基础学完感想

    开学后的一个多月因为要准备acm省赛,所以docnet视频基本没看了!不过,虽然在省赛前每天都在做题,赛前刷了80多题吧!!但是比赛的时候就3题,渣啊!只做出了3个水题,后面两个小时搞两题就是出不来, ...

  4. .Net之一般处理程序

    1.一般处理程序是什么? 答:一般处理程序是以.ashx结尾的文件,默认命名为Handler1.ashx. 用在Web项目中,也就是我们常说的网站项目. 2.新建一个一般处理程序 1.1 新建一个空网 ...

  5. 第一节 生命周期和Zend引擎

    一切的开始: SAPI接口 SAPI(Server Application Programming Interface)指的是PHP具体应用的编程接口, 就像PC一样,无论安装哪些操作系统,只要满足了 ...

  6. .net 更改日期格式

    示例:更改日期格式 下面的代码示例使用 Regex.Replace 方法将 mm/dd/yy 格式的日期替换为 dd-mm-yy 格式的日期. static string MDYToDMY(strin ...

  7. 07-C语言流程控制if、switch

    目录: 一.流程控制 条件分支 if else 二.流程控制 开关分支 switch 回到顶部 一.流程控制 条件分支 1.语法格式:if(表达式1){ //表达式1为真(非0时),执行的语句部分. ...

  8. GraphLab:新的面向机器学习的并行框架

    大规模图数据计算引起了许多知名公司的关注,微软提出了用于图数据匹配的Horton - Querying Large Distributed Graphs(Link:http://research.mi ...

  9. RESTClient 控件 从服务器获得数据集 REST

    用TRESTClient控件调用REST架构服务 RESTClient控件返回数据集 用到的控件 RESTClient RESTRequest RESTResponseDataSetAdapter p ...

  10. 贫血模型or领域模型

    参考: http://lifethinker.iteye.com/blog/283668 http://www.uml.org.cn/mxdx/200907132.asp http://www.itu ...