hdu------(3549)Flow Problem(最大流(水体))
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8203 Accepted Submission(s): 3817
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
HyperHexagon's Summer Gift (Original tasks)
代码:
1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<queue>
5 using namespace std;
6 const int inf=0x3f3f3f3f;
7 int mat[30][30];
8 int dist[31];
9 int n,m;
10 int min(int a,int b)
11 {
12 return a<b?a:b;
13 }
14 bool bfs(int st,int to){
15 memset(dist,-1,sizeof(dist));
16 queue<int>q;
17 q.push(st);
18 dist[st]=0;
19 int t;
20 while(!q.empty()){
21 t=q.front();
22 q.pop();
23 for(int i=1;i<=n;i++){
24 if(dist[i]<0&&mat[t][i]>0){
25 dist[i]=dist[t]+1;
26 if(i==to)return 1;
27 q.push(i);
28 }
29 }
30 }
31 return 0;
32 }
33 int dfs(int st,int to,int flow)
34 {
35 int tem;
36 if(st==to||flow==0) return flow;
37 for(int i=1;i<=n;i++){
38 if((dist[i]==dist[st]+1)&&mat[st][i]>0&&(tem=dfs(i,to,min(mat[st][i],flow))))
39 {
40 mat[st][i]-=tem;
41 mat[i][st]+=tem;
42 return tem;
43 }
44 }
45 return 0;
46 }
47 int Dinic(int st,int en)
48 {
49 int ans=0;
50 while(bfs(st,en))
51 ans+=dfs(st,en,inf);
52 return ans;
53 }
54 int main(){
55 int cas,i,a,b,c;
56 scanf("%d",&cas);
57 for(i=1;i<=cas;i++){
58 scanf("%d%d",&n,&m);
59 memset(mat,0,sizeof(mat));
60 while(m--){
61 scanf("%d%d%d",&a,&b,&c);
62 mat[a][b]+=c;
63 }
64 printf("Case %d: %dn",i,Dinic(1,n));
65 }
66 return 0;
67 }
- 未来3年,人工智能如何影响法律行业?5位权威专家给出趋势
- Java 常见内存溢出异常与代码实现
- nginx限制上传大小和超时时间设置说明/php限制上传大小
- Unity Application Block 1.2 学习笔记
- 苹果首个自动驾驶专利到底有什么来头?
- 围棋遇上互联网:科技打开优秀传统文化未来之门
- 神经网络开始放飞自我!都是因为架构搜索新算法
- 浏览器缓存问题的解决
- nginx下目录浏览及其验证功能、版本隐藏等配置记录
- Sqlite的多表连接更新
- Enterprise Library 4.1学习笔记6----加密应用程序块
- 浅谈数据库主键策略
- nginx应用总结(1)--基础认识和应用配置
- nginx反向代理tomcat访问时浏览器加载失败,出现 ERR_CONTENT_LENGTH_MISMATCH 问题
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- CentOS桌面环境中网卡启动失败的解决方法
- 浅谈Linux vfork与fork简单对比分析
- Linux定时任务Crontab的使用方法
- linux虚拟网络设备之vlan配置详解
- Centos7安装完后无法联网的解决方法
- Linux静态链接库与模板类的处理方式
- 深入研究RocketMQ消费者是如何获取消息的
- CentOS7下 Apache的安装配置方法
- expect命令在linux下实现批量ssh免密
- 学习Vim合并行的方法和技巧
- 九种查找算法
- 代码防御性编程的十条技巧
- Ubuntu终端多窗口分屏Terminator
- linux 编译安装python3.6的教程详解
- Unix/Linux系统下的nobody用户与nologin详细介绍