Flow Problem

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

Total Submission(s): 6987    Accepted Submission(s): 3262

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

解题报告

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 30
#define inf 99999999
using namespace std;
int n,m,a[N],flow,pre[N];
int Edge[N][N];
queue<int >Q;
int bfs()
{
while(!Q.empty())
Q.pop();
memset(a,0,sizeof(a));
memset(pre,0,sizeof(pre));
Q.push(1);
pre[1]=1;
a[1]=inf;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int v=1;v<=n;v++)
{
if(!a[v]&&Edge[u][v]>0)
{
pre[v]=u;
a[v]=min(Edge[u][v],a[u]);
Q.push(v);
}
}
if(a[n])break;
}
if(!a[n])
return -1;
else return a[n];
}
void ek()
{
int a,i;
while((a=bfs())!=-1)
{
for(i=n;i!=1;i=pre[i])
{
Edge[pre[i]][i]-=a;
Edge[i][pre[i]]+=a;
}
flow+=a;
}
}
int main()
{
int t,i,j,u,v,w,k=1;
scanf("%d",&t);
while(t--)
{
flow=0;
memset(Edge,0,sizeof(Edge));
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
Edge[u][v]+=w;
}
ek();
printf("Case %d: ",k++);
printf("%d\n",flow);
}
return 0;
}

HDU3549_Flow Problem(网络流/EK)的更多相关文章

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

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

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

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

  3. 网络流EK

    #include <iostream> #include <queue> #include <string.h> #define MAX 302 using nam ...

  4. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  5. hdu 3549 Flow Problem 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Network flow is a well-known difficult problem f ...

  6. POJ-3436 ACM Computer Factory(网络流EK)

    As you know, all the computers used for ACM contests must be identical, so the participants compete ...

  7. 网络流Ek算法

    例题:  Flow Problem HDU - 3549 Edmonds_Karp算法其实是不断找增广路的过程. 但是在找的过程中是找"最近"的一天增广路, 而不是找最高效的一条增 ...

  8. 【HDU5772】String Problem [网络流]

    String Problem Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Ou ...

  9. [bzoj3218]a + b Problem 网络流+主席树优化建图

    3218: a + b Problem Time Limit: 20 Sec  Memory Limit: 40 MBSubmit: 2229  Solved: 836[Submit][Status] ...

随机推荐

  1. ftk学习记(label篇)【转】

    转自:http://blog.csdn.net/feixiaoxing/article/details/25000093 版权声明:本文为博主原创文章,未经博主允许不得转载. [ 声明:版权所有,欢迎 ...

  2. golang xorm reverse 自动生成数据库实体文件

    一.先安装好需要的东西 xorm 也可以参考官方文档 readme.md https://github.com/go-xorm/cmd 和 http://xorm.io/docs/ go get gi ...

  3. AC日记——文艺平衡树 洛谷 P3391

    文艺平衡树 思路: splay翻转操作模板: 虚拟最左最右端点,然后每次都把l翻转到root,r+2翻转到root的右节点: 然后在r+2的左节点上打标记: 标记需要在旋转,rank,print时下放 ...

  4. NYOJ 20.吝啬的国度-DFS+STL(vector保存上一节点)

    整理代码 吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第 ...

  5. Codeforces 761C Dasha and Password(枚举+贪心)

    题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...

  6. centos7最新 mysq5.6 安装

    安装教程  1.下载mysql的repo源   wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 可能遇见的问题: ...

  7. 书写一个程序,把变量n的初始值设置为1957,然后利用除法运算和取余运算把变量n的每一位数字都抽出来并打印

    class number { void num(){ int a,b,c,d; int n=1957; a=n/1000; b=n/100%10; c=n/10%10; d=n%10; System. ...

  8. Linux运维:CentOS6和7的区别

    Liunx笔记:CentOS6和CentOS7的区别 路飞学城运维人员 在线流程图软件 Ago linux运维群: 93324526 笔者QQ:578843228 常用安装包下载 yum instal ...

  9. [原创]Getting Started with Skywalking

    Getting Started with Skywalking Pre JDK 1.8+ Skywalking(v3.2.6) (All packages can be download from G ...

  10. oracle查询、删除表中相同的数据

    delete FROM tablename a WHERE rowid > ( SELECT min(rowid) FROM tablename b WHERE b.id = a.id and ...