Flow Problem HDU - 3549

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. 

InputThe 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)OutputFor 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 题意:网络流模版题,1到n的结点的最大的流
思路:用dinic
#include<stdio.h>
#include<iostream>
#include<map>
#include<string.h>
#include<queue>
#include<vector>
#include<math.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int dis[];
int flow[][];
int n,m;
int bfs()
{
memset(dis,-,sizeof(dis));
queue<int>Q;
dis[]=;
Q.push();
while(!Q.empty())
{
int top=Q.front();
Q.pop();
for(int i=;i<=n;i++)
{
if(flow[top][i]>&&dis[i]<)
{
dis[i]=dis[top]+;
Q.push(i);
}
}
}
if(dis[n]>)return ;
return ;
}
int dinic(int x,int k)
{
if(x==n)
return k;
int y;
for(int i=;i<=n;i++)
{
if(flow[x][i]>&&dis[i]==dis[x]+&&(y=dinic(i,min(k,flow[x][i]))))
{
flow[x][i]-=y;
flow[i][x]+=y;
return y;
}
}
return ; }
int main()
{
int t;
scanf("%d",&t);
int cas=;
while(t--)
{
memset(flow,,sizeof(flow));
scanf("%d%d",&n,&m);
int a,b,c;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
flow[a][b]+=c;
}
int ans=;
while(bfs())
{
//cout<<"++"<<endl;
int res;
while(res=dinic(,inf))ans+=res;
}
printf("Case %d: %d\n",cas++,ans);
}
}

用sap加邻接矩阵,maze那里是叠加,不是覆盖

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e3+;
const int mod = 1e9+; int maze[maxn][maxn];
int gap[maxn],dis[maxn],pre[maxn],cur[maxn]; int sap(int start,int end ,int nodenum)
{
memset(cur,,sizeof cur);
memset(dis,,sizeof dis);
memset(gap,,sizeof gap);
int u = pre[start] = start,maxflow = ,aug = -;
gap[] = nodenum;
while(dis[start] < nodenum)
{
loop:
for(int v = cur[u];v < nodenum; v++)
if(maze[u][v] && dis[u] == dis[v] + ){
if(aug == - || aug > maze[u][v])
aug = maze[u][v];
pre[v] = u;
u = cur[u] = v;
if(v == end)
{
maxflow += aug;
for(u = pre[u]; v!=start;v=u,u=pre[u])
{
maze[u][v] -= aug;
maze[v][u] += aug;
}
aug = -;
}
goto loop;
}
int mindis = nodenum - ;
for(int v = ;v<nodenum;v++)
if(maze[u][v] && mindis > dis[v])
{
cur[u] = v;
mindis = dis[v];
}
if((--gap[dis[u]]) == )
break;
gap[dis[u] = mindis + ]++;
u = pre[u]; }
return maxflow;
} int main()
{
int t;
scanf("%d",&t);
for(int ca = ;ca <= t; ca++)
{
memset(maze,,sizeof maze);
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int a,b,l;
scanf("%d%d%d",&a,&b,&l);
maze[a-][b-] += l;
}
int res = sap(,n-,n);
printf("Case %d: %d\n",ca,res);
}
}

Flow Problem HDU - 3549的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

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

  2. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

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

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

  4. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  5. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  6. HDU 3549 Flow Problem 网络流(最大流) FF EK

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

  7. hdu 3549 Flow Problem (网络最大流)

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

  8. HDU 3549 基础网络流EK算法 Flow Problem

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit ...

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

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

随机推荐

  1. SpringBoot | 第三十七章:集成Jasypt实现配置项加密

    前言 近期在进行项目安全方面评审时,质量管理部门有提出需要对配置文件中的敏高文件进行加密处理,避免了信息泄露问题.想想前段时间某公司上传github时,把相应的生产数据库明文密码也一并上传了,导致了相 ...

  2. webpack 3 优化

    编译时间太长 项目为多页面应用时,编译的时候每个入口都会读取依赖的路径,所以入口越多,会导致编译越慢 公用库提取 除了公用的框架(如 Vue.React)以外,不同页面所需要的第三方库可能不一样,而且 ...

  3. Vue系列(1):单页面应用程序

    前言:关于页面上的知识点,如有侵权,请看 这里 . 关键词:SPA.单个 HTML 文件.全靠 JS 操作.Virtual DOM.hash/history api 路由跳转.ajax 响应.按需加载 ...

  4. 改善WPF应用程序性能的10大方法 (转发)

    WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...

  5. centos 安装 freeswitch,开启与关闭

    ---恢复内容开始--- 官网说明地址 :https://freeswitch.org/confluence/display/FREESWITCH/CentOS+7+and+RHEL+7 1.获取源码 ...

  6. JAVA时间加工类

    /** * 当天凌晨 */ public static Calendar startOfDay(Calendar c) { if (c == null) { return c; } c.set(Cal ...

  7. js 数组方法大集合,各方法是否改变原有的数组详解

    不会改变原来数组的有: concat()---连接两个或更多的数组,并返回结果. every()---检测数组元素的每个元素是否都符合条件. some()---检测数组元素中是否有元素符合指定条件. ...

  8. axios跨域问题记录

    axios({headers: {'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/json; charset=UTF ...

  9. MacOS内核调试环境搭建

    http://ddeville.me/2015/08/using-the-vmware-fusion-gdb-stub-for-kernel-debugging-with-lldb http://dd ...

  10. 【luogu P1637 三元上升子序列】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1637 BIT + 离散化. 读题得数据规模需离散化.BIT开不到longint这么大的数组. 对于题目所求的 ...