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. iOS面试题03-UI控件

    UI控件面试题 1.怎么解决缓存池端的问题(cell) 回答:1.>OS中不存在缓存池的情况,因为通常我们iOS开发,对象都是在需要的时候才会创建, 有种常用的说话叫做懒加载,还有在UITabl ...

  2. 创建Xml的将但方法和向Xml中添加数据

    </SendUserId>// ::</DateTime></AcceptUserId>       <AcceptUserId></Accept ...

  3. VS2010使用DX报错 VS报错之混合模式程序集是针对“v1.1.4322”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。

    更改项目的app.config内容为以下内容 目的是开启对低版本的NETFWK支持 其实出现混合模式集的问题不只是在V1.1.4322这个版本上,在查询解决方案时发现,但凡程序集版本发生改变时,都有可 ...

  4. 帝国cms7.0 列表模板调用本栏目缩略图

    [e:loop={"select classimg from phome_enewsclass where classid='$GLOBALS[navclassid]'",1,24 ...

  5. jquery ajax方法和其他api回顾

    >> $ele.load(url, data, callback); //带data参数时为POST请求,否则为GET请求 // $('#testload').load('data.htm ...

  6. 韦根(Wiegand)数据传输格式

    韦根数据传输使用TTL电平,有两条数据线,分别称为DATA0和DATA1.无数据传输时,两条线都是高电平,当传输“1”时,DATA0为高,DATA1为低:当传输“0”时,DATA0为低,DATA1为高 ...

  7. Python的安装和详细配置(转)

    Python是一种面向对象.解释型计算机程序设计语言.被认为是比较好的胶水语言.至于其他的,你可以去百度一下.本文仅介绍python的安装和配置,供刚入门的朋友快速搭建自己的学习和开发环境.本人欢迎大 ...

  8. Warning: Unable to send packet: Error with PF_PACKET send() [11]: Message too long (errno = 90)

    今天在使用tcpreplay重放流量时,发现有的数据包没有发送成功: Warning: Unable to send packet: Error with PF_PACKET send() [215] ...

  9. hdu1711(终于搞懂了KMP算法了。。)

    题意:给你两个长度分别为n(1 <= N <= 1000000)和m(1 <= M <= 10000)的序列a[]和b[],求b[]序列在a[]序列中出现的首位置.如果没有请输 ...

  10. IT 名企招聘信息

    [搜狗]网页搜索抓取与挖掘组诚聘实习生 工作方向: 抓取架构方向. 职位要求  1.对互联网和搜索引擎技术有浓厚兴趣: 2.熟练使用c .熟悉Linux开发环境.熟悉shell.python.awk等 ...