Flow Problem

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
 

————————————————————————————————————————————————————————


题目要求很简单,求从第一个点到最后一个点的最大流量。
最大流板子题,直接套模板,

Dinic
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500 struct node
{
int u, v, next, cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt; void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
} void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
} bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
} int DFS(int x, int exp, int ee)
{
if (x == ee||!exp) return exp;
int temp,flow=0;
for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
{
int v = edge[i].v;
if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
} int Dinic_flow(int ss, int ee)
{
int ans = 0;
while (BFS(ss, ee))
{
for (int i = 0; i <= ee; i++) nt[i] = s[i];
ans+= DFS(ss, INF, ee);
}
return ans;
} int main()
{
int n,m,u,v,c,T;
int q=1;
scanf("%d",&T);
while (T--)
{
scanf("%d %d", &n, &m);
init();
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&c);
add(u,v,c);
}
printf("Case %d: %d\n",q++,Dinic_flow(1,n)); }
return 0;
}

isap+bfs
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
using namespace std;
const int MAXN =100010;//点max
const int MAXM=400010;//边max
const int INF=0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
} edge[MAXM];
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0)
{
edge[tol].to=v;
edge[tol].cap=w;
edge[tol].flow=0;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=rw;
edge[tol].flow=0;
edge[tol].next=head[v];
head[v]=tol++;
} int Q[MAXN];
void BFS(int start,int end)
{
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=1;
int front=0,rear=0;
dep[end]=0;
Q[rear++]=end;
while(front!=rear)
{
int u=Q[front++];
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(dep[v]!=-1)
continue;
Q[rear++]=v;
dep[v]=dep[u]+1;
gap[dep[v]]++;
}
}
} int S[MAXN];
int sap(int start,int end,int N)
{
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top=0;
int u =start;
int ans=0;
while(dep[start]<N)
{
if(u==end)
{
int Min=INF;
int inser;
for(int i=0; i<top; i++)
{
if(Min>edge[S[i]].cap-edge[S[i]].flow)
{
Min=edge[S[i]].cap-edge[S[i]].flow;
inser=i;
}
} for(int i=0; i<top; i++)
{
edge[S[i]].flow+=Min;
edge[S[i]^1].flow-=Min; }
ans+=Min;
top=inser;
u=edge[S[top]^1].to;
continue;
} bool flag=false;
int v;
for(int i=cur[u]; i!=-1; i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
{
flag=true;
cur[u]=i;
break;
} }
if(flag)
{
S[top++]=cur[u];
u=v;
continue;
}
int Min=N;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
{
Min=dep[edge[i].to];
cur[u]=i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u]=Min+1;
gap[dep[u]]++;
if(u!=start)
u=edge[S[--top]^1].to;
}
return ans; } int main()
{
int n,m,u,v,w,T;
while(~scanf("%d",&T))
{
int q=0;
while(T--)
{
scanf("%d%d",&m,&n);
init();
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,0);
}
int ans=sap(1,m,m);
printf("Case %d: %d\n",++q,ans);
}
} return 0;
}

Hdu3549 Flow Problem 2017-02-11 16:24 58人阅读 评论(0) 收藏的更多相关文章

  1. Hardwood Species 分类: POJ 树 2015-08-05 16:24 2人阅读 评论(0) 收藏

    Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 20619 Accepted: 8083 De ...

  2. hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏

    a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...

  3. 团体程序设计天梯赛L3-010 是否完全二叉搜索树 2017-03-24 16:12 29人阅读 评论(0) 收藏

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  4. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  5. hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏

    use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...

  6. Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  7. C语言之void类型及void指针 分类: C/C++ 2015-07-13 11:24 8人阅读 评论(0) 收藏

    原文网址:http://www.cnblogs.com/pengyingh/articles/2407267.html 1.概述 许多初学者对C/C 语言中的void及void指针类型不甚理解,因此在 ...

  8. 初学Larevel 2014-08-21 11:24 90人阅读 评论(0) 收藏

    添加第一个路由时就遇到了 404错误,查了一下说要这样才能 版权声明:本文为博主原创文章,未经博主允许不得转载.

  9. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

随机推荐

  1. bzoj4891: [Tjoi2017]龙舟

    求$\frac{b_1b_2b_3...b_m}{a_1a_2a_3...a_m}\%M$ M<=1e18,m<=100000,数据组数<=50 用pollard-rho分解M的质因 ...

  2. 【精华】部署与管理ZooKeeper(转)

    部署与管理ZooKeeper(转) 本文以ZooKeeper3.4.3版本的官方指南为基础:http://zookeeper.apache.org/doc/r3.4.3/zookeeperAdmin. ...

  3. 1021 docker prometheus监控体系

    jmeter plugin监控的信息很少,只有cpu.内存.网络IO,但这些是不够的.例如对于分析mysql数据库的慢查询.最大连接数等更加细密度的信息. 服务端稳定测试的三个前提: 1.应用级别的自 ...

  4. 安卓控件获取器uiautomatorviewer初体验:"unable to connect to the adb. check if adb is installed correctly"

    解决方法:转自:https://plus.google.com/108487870030743970488/posts/2TrMqs1ZGQv Challenge Accepted:1. Screen ...

  5. java io之管道流

    一.java.io中存在一中流叫管道流,类似管道的功能.PipedOutputStream和PipedInputStream分别是管道输出流和管道输入流.这个两个流必须同时使用. 二.作用:进行两个线 ...

  6. jquery对象和DOM对象转换

    DOM对象转换成jquery对象 var box = document.getElementById('box'); console.log($(box)); jquery对象转化成DOM对象 第一种 ...

  7. 5_python之路之员工管理系统

    python之路之员工管理系统 1.程序说明:Readme.cmd 1.程序文件:info_management.py user_info 2.程序文件说明:info_management.py-主程 ...

  8. 常见的JS和CSS问题

    事件冒泡 DOM的事件冒泡机制和WPF很相似,DOM事件机制包含冒泡和捕获两种,按照topmost element->innermost element方向传递事件被称为捕获方式,而从inner ...

  9. spring jpa sqls

    package com.example.repository; import java.util.List; import org.springframework.data.jpa.repositor ...

  10. JAVA 非对称加密工具

    import java.io.File; import java.io.FileInputStream; import java.math.BigInteger; import java.securi ...