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. jQuery1.4与json格式兼容问题

    原文发布时间为:2010-10-10 -- 来源于本人的百度文章 [由搬家工具导入] 原来使用jQuery1.3.2编写的代码,更换到1.4.2后,使用jQuery.ajax()加载的json文件,不 ...

  2. Selenium2+python自动化(学习笔记3)

    1.加载firefox配置 参考代码: # coding=utf-8from selenium import webdriver# 配置文件地址,打开Firefox点右上角设置--帮助--故障排除信息 ...

  3. locust性能测试(无 web ui 模式)

    前言 前面是在web页面操作,需要手动的点start启动,结束的时候也需要手工去点stop,没法自定义运行时间,这就不太方便.locust提供了命令行运行的方法,不启动web页面也能运行,这就是no- ...

  4. 用户找回密码功能JS验证邮箱通过点击下一步隐藏邮箱输入框并修改下一步按钮的ID

    //这里是BaseDao /** * 获得一个对象 * @param hql * @param param * @return */ public Object get(String hql, Obj ...

  5. Codeforces Gym100971 C.Triangles-组三角形 (IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13)

    这个题就是组三角形,从给出的数组里任选两个和未知的边组三角形. 任意两边之和大于第三边,记住这个就可以了. 代码: 1 #include<cstdio> 2 #include<cst ...

  6. Codeforces Gym100971 B.Derangement (IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13)

    昨天训练打的Gym,今天写题解. 这个题就是输出的时候有点小问题,其他的都很简单. 代码: #include<iostream> #include<cstring> #incl ...

  7. [Machine Learning with Python] My First Data Preprocessing Pipeline with Titanic Dataset

    The Dataset was acquired from https://www.kaggle.com/c/titanic For data preprocessing, I firstly def ...

  8. 牛客网暑期ACM多校训练营 记录

    所有牛客多校的做题记录请右转队伍wiki Name Date Rank 2018 Multi-University,Nowcoder Day 1 2018.7.19 16/338 5/10 https ...

  9. 安全搜索引擎Shodan(搜蛋)命令行模式使用TIPS

    https://www.shodan.io/ 与谷歌通过网址来搜索互联网的方式不同,Shodan通过互联网背后的通道来搜索信息.它就象是一种“黑暗”的谷歌,不断在寻找服务器.网络摄像头.打印机.路由器 ...

  10. mysql之创建数据库,创建数据表

    写在前面 项目中用到mysql数据库,之前也没用过mysql,今天就学下mysql的常用的语法,发现跟sql server的语法极其相似.用起来还是蛮简单的. 一个例子 1.创建一个名为School的 ...