Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 11733    Accepted Submission(s): 5565

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
 
Source
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  1532 3572 3416 3081 3491

数组大小也是一门学问啊

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 200+10
#define INF 10000000+10
struct node
{
int u,v,cap,flow,next;
}edge[2100];
int head[MAX],cur[MAX];
int vis[MAX],dis[MAX],m,n,top;
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{
node E1={a,b,c,0,head[a]};
edge[top]=E1;
head[a]=top++;
node E2={b,a,0,0,head[b]};
edge[top]=E2;
head[b]=top++;
}
bool bfs(int s,int e)
{
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
queue<int>q;
while(!q.empty()) q.pop();
q.push(s);
vis[s]=1;
dis[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v]&&E.cap>E.flow)
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+1;
if(E.v==e)
return true;
q.push(E.v);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(x==e||a==0) return a;
int flow=0,f;
for(int i=cur[x];i!=-1;i=edge[i].next)
{
node& E=edge[i];
if(dis[x]+1==dis[E.v]&&(f=dfs(E.v,min(a,E.cap-E.flow),e))>0)
{
E.flow+=f;
flow+=f;
edge[i^1].flow-=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
memcpy(cur,head,sizeof(head));
flow+=dfs(s,INF,e);
}
return flow;
}
void getmap()
{
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
}
int main()
{
int t;
int k=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
printf("Case %d: %d\n",k++,MAXflow(1,n));
}
return 0;
}

hdoj--3549--Flow Problem(最大流)的更多相关文章

  1. hdoj 3549 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. hdoj 3549 Flow Problem(最大网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 思路分析:该问题为裸的最大网络流问题,数据量不大,使用EdmondsKarp算法求解即可:需要注 ...

  4. hdu 3549 Flow Problem 最大流 Dinic

    题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...

  5. hdu - 3549 Flow Problem (最大流模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...

  6. HDU 3549 Flow Problem(最大流)

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

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

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

  8. hdu 3549 Flow Problem

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

  9. 网络流 HDU 3549 Flow Problem

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

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

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

随机推荐

  1. ES 新增字符串方法

    话不多说,直接开鲁 1. startsWith() 作用: 检测字符串以什么开头 实例: let str = "www.qjzzj.top"; console.log(str.st ...

  2. Valgrind的安装及简单使用

    1.获取源码 wget http://www.valgrind.org/downloads/valgrind-3.14.0.tar.bz2 2.解压缩 tar -jxvf valgrind-3.14. ...

  3. Adobe Flex迷你教程 —Flex4全屏显示

    应用场景 1.播放器 我们经常看视频的时候,需要全屏显示,(在flex中这个视频初始化的时候是嵌入到html的iframe中). 2.监控 如下图所示,大多时候我们的监控用的是flex,而树形菜单和标 ...

  4. C++ 学习笔记(一些新特性总结3)

    C++ 学习笔记(一些新特性总结3) public.protected 和 private 继承 public 继承时,基类的存取限制是不变的. class MyClass { public: // ...

  5. moble 设备多指手势识别 (tap , double_tap , pinch)

    function(){ elem.addEventListener('touchstart', start , false) elem.addEventListener('touchend', end ...

  6. SqlServer 错误日志切换和查看

    Sql Server 日志 和 代理错误日一般在实例重新启动后自己主动切换,假设实例久未重新启动,将可能积累太多的日志,不方便查看. 查看错误日志大小: --查看日志大小 EXEC xp_enumer ...

  7. sass03 变量、样式导入

    demo1.scss @import "css.css"; //导入css文件 @import "http://ss/xx"; //导入css文件 @impor ...

  8. WebAPI的自动化监控和预警

    Metrics.net + influxdb + grafana 构建WebAPI的自动化监控和预警 前言 这次主要分享通过Metrics.net + influxdb + grafana 构建Web ...

  9. ajax无刷新翻页后,jquery失效问题的解决

    例如 $(".entry-title a").click(function () {   只对第一页有效, 修改为 $(document).on('click', ".e ...

  10. links[v1]

    justep core java Spring Boot ui5 template spring Cross-origin resource sharing 统一异常处理 数据库连接池的选择 Drui ...