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. XML 文档解析操作

    sing System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security; ...

  2. git版本工具(团队开发常用)

    1.创建一个版本库 mkdir repository    //创建一个文件夹 git init        //把目录编程git可以管理的仓库 2.提交文件到版本库 git add test.tx ...

  3. HOOK API(四)—— 进程防终止

    HOOK API(四) —— 进程防终止 0x00        前言 这算是一个实战吧,做的一个应用需要实现进程的防终止保护,查了相关资料后决定用HOOK API的方式实现.起初学习HOOK API ...

  4. 在mac系统安装Apache Tomcat的详细步骤(转载自himi的博客,修改了错误添加了图片)

    链接地址:http://blog.csdn.net/liuyuyefz/article/details/8072485 1. 2. 3. 4. 5. 对于Apache Tomcat 估计很多童鞋都会, ...

  5. codeforces 613A. Peter and Snow Blower

    题目链接 给一个多边形, 一个多边形外的定点, 求这个点距离多边形的最短距离和最长距离. 最长距离肯定是和某个顶点的连线, 而最短距离是和点的连线或是和某条边的连线. 对于一条边上的两个点a, b, ...

  6. cdoj 1134 男神的约会 状压dp

    题目链接 给一个10*10的方格, 每个格子里面有0-9,走到一个格子, 就要在这个格子待一段时间, 时间长度为这个格子的数字. 从左上角走到右下角, 要求0-9必须每种格子都要走到, 输出最短时间. ...

  7. python数据库连接

    现在装python基本都内置了sqlite连接,写成如下形式即可 from sqlite3 import dbapi2 as sqlite 如果需要insert或update东西,之后的cur必须co ...

  8. 阿里云ECS每天一件事D1:配置SSH

    近期因为项目需求,采购了两台阿里云ECS,选择的系统为CentOS 6.3 X64 安全加固版,额外买了160G的硬盘,应该够应付此项目的需求了. ECS默认已经配置好了sshd服务,可以使用root ...

  9. js获取浏览器窗口的大小

    在我本地测试当中: 在IE.FireFox.Opera下都可以使用 document.body.clientWidth document.body.clientHeight 即可获得,很简单,很方便. ...

  10. WIZnet即将推出高性能网络芯片W5500

    WIZnet将于9月份推出高性能网络芯片W5500,这是继W5100.W5200和W5300之后一款全新的全硬件TCP/IP协议栈网络芯片,这款芯片具有更低功耗与工作温度,及改良工艺,是嵌入式以太网的 ...