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. 不用框架使用ajax 纯js使用ajax post,get范例及其区别

    原文发布时间为:2009-11-15 -- 来源于本人的百度文章 [由搬家工具导入] 不用框架使用ajax 纯js使用ajax post,get范例及其区别 ===================== ...

  2. mvc filters

    1.controller using System; using System.Collections.Generic; using System.Linq; using System.Web; us ...

  3. StringTokenizer:字符串分隔用法简介

    StringTokenizer:字符串分隔解析类型 属于:java.util包. 1.构造函数. 1. StringTokenizer(String str) :构造一个用来解析str的StringT ...

  4. Day 29 process&thread_1

    进程和线程 1 进程(process): 1.定义: 最小的执行单元.进程就是一个程序在一个数据集上的一次动态执行过程. 进程一般由程序.数据集.进程控制块三部分组成: 我们编写的程序用来描述进程要完 ...

  5. HDU 5135.Little Zu Chongzhi's Triangles-字符串 (2014ACM/ICPC亚洲区广州站-重现赛)

    Little Zu Chongzhi's Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 ...

  6. luogu P1651 塔

    题目描述 小明很喜欢摆积木,现在他正在玩的积木是由N个木块组成的,他想用这些木块搭出两座高度相同的塔,一座塔的高度是搭建它的所有木块的高度和,并且一座塔至少要用一个木块.每个木块只能用一次,也可以不用 ...

  7. CPU、内存、IO虚拟化关键技术及其优化探索

    https://yq.aliyun.com/articles/71295?spm=5176.8091938.0.0.3LQ7NM

  8. 快速销售订单 - OM:销售订单表单:级联行题头更改

    PROFILE OM:销售订单表单:级联行题头更改

  9. MATLAB矩阵基础运算

    1.矩阵的加减运算X+Y或者X-Y:元素对应相加减.在此要求X与Y必须是同维矩阵,否则会出现错误信息.如果Y为一个数,则表示X中每一元素+Y或者-Y: %当X和Y都为同维矩阵时 X=[1,2,-3;2 ...

  10. ubuntu git 下添加 ssh

    1 ssh-keygen 产生公钥和私钥  2 cat ~/.ssh/id_rsa.pub 复制出对应的公钥, 3 拷贝到git上的ssh-key里,注意拷贝时,换行处可能会引起错误,要把换行处的空格 ...