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. spring+hibernate基础

    把数据库的配置信息写在一个文件中 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:3306/ ...

  2. 【转载】设备坐标(窗口/window)和逻辑坐标(视口/viewport)

    一.预备知识 1.窗口是基于逻辑坐标的. 2.视口是基于设备坐标. 3.设备坐标是以像素为单位的,逻辑坐标是以.cm,m,mm,..... 4.系统最后一定要把逻辑坐标变为设备坐标. 5.设备坐标有3 ...

  3. PHP PDO select语句结果行数计算

    PDO有一个函数PDOStatement::rowCount返回上一个SQL语句影响的行数. rowCount函数对于DELETE, INSERT, 或者UPDATE语句的结果是正确的,但对于sele ...

  4. 一,PHP 语法

    基本的 PHP 语法 PHP 的脚本块以 <?php 开始,以 ?> 结束.您可以把 PHP 的脚本块放置在文档中的任何位置. 当然,在支持简写的服务器上,您可以使用 <? 和 ?& ...

  5. js运算符(运算符的结合性)

    1.javascript具有下列种类的运算符:算术运算符;逻辑运算符;比较运算符; 2.目的分类:字符串运算符;逻辑运算符;逐位运算符;赋值运算符; 3.特殊运算符:条件运算符;typeof运算符;创 ...

  6. Node Node

    http://www.nodejs.org/ http://outofmemory.cn/code-snippet/1403/node-javascript-classic-introduction- ...

  7. Qt在Linux环境下应用程序字体模糊的解决方法(先改成使用默认字体,然后使用qtconfig配置)

    这两天一直在用Qt实现一个跨平台的软件.软件之前在Windows上编写的,后来放到里Ubuntu 10.10下编译.程序运行时遇到一个很棘手的问题,界面文本非常模糊.后来在网上查阅了好几天的资料,经历 ...

  8. Oracle 使用RMAN

    RMAN 数据库备份 特点: . 跳过未使用的数据块 . 备份压缩 . 执行增量备份 . 块级别的恢复 组件: . RMAN命令执行器(RMAN Executable) . 目标数据库(Traget ...

  9. android LinearLayout和RelativeLayout实现精确布局

    先明确几个概念的区别: padding margin:都是边距的含义,关键问题得明白是什么相对什么的边距padding:是控件的内容相对控件的边缘的边距. margin  :是控件边缘相对父空间的边距 ...

  10. Linux设置高分辨率后无法进入X系统

    Vmware9.0中Xubuntu分辨率从800x600变更为1366x768后在用户输入密码登录后会自动退出x系统,出现这种情况时可以切换到命令行登录界面,然后将-/.config/xfce4/xf ...