HDU3549_Flow Problem(网络流/EK)

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;
}
时间: 2025-01-12 16:54:19

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

HDU1532_Drainage Ditches(网络流/EK模板)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8599    Accepted Submission(s): 4005 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie's

POJ1149_PIGS(网络流/EK)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15721   Accepted: 7021 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

HDU 4975 A simple Gaussian elimination problem. 网络流+矩阵上的dp

随机输出保平安啊 和hdu4888一个意思,先跑个网络流然后dp判可行. ==n^3的dp过不了,所以把n改成200. ==因为出题人没有把多解的情况放在200*200以外的矩阵. #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int MAX_N = 12

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

欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 10184    Accepted Submission(s): 4798 Problem Description Network flow is a well-known difficult pro

hdu 3549 Flow Problem 网络流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 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

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

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory. Every ACM computer consists of P parts. When all these

初涉网络流[EK&amp;dinic]

主要还是板子 Edmonds-Karp 从S开始bfs,直到找到一条到达T的路径后将该路径增广,并重复这一过程. 在处理过程中,为了应对“找到的一条路径把其他路径堵塞”的情况,采用了建反向弧的方式来实现“反悔”过程. 这种“反悔”的想法和技巧值得借鉴. 1 int maxFlow() 2 { 3 int ret = 0; 4 for (;;) 5 { 6 memset(f, 0, sizeof f); 7 memset(bck, 0, sizeof bck); 8 std::queue<int>

计蒜客2016复赛 菜鸟物流的运输网络 网络流EK

题源:https://nanti.jisuanke.com/t/11215 分析:这题是一个比较经典的网络流模型.把中间节点当做源,两端节点当做汇,对节点进行拆点,做一个流量为 22 的流即可. 吐槽:这是官方题解,然后其实赛场上谢了这个解法,但是我写搓了,因为最后输出路径的时候傻逼了 我居然向最短路一样记录前驱输出路径,简直傻逼了 着重强调(对我自己):网络流的一次增广是需要记录前驱的,但是多次增广以后,可以反向流,前驱就不对了 所以要判断哪些边,在最大流上时,需要枚举边,看哪个满流,然后随便

UVA 10779 Collectors Problem 网络流+建图

题目链接:点击打开链接 题意:白书P370 思路: 因为问的是最后贴纸总数,那么就设最后的贴纸总数是网络流的答案. 首先我们模拟贴纸的流动过程: Bob 的 某种贴纸a -> 给一个没有a贴纸的人Peo -> 还给Bob一个Peo的某张重复贴纸 -> 这张贴纸可以算作答案了 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include