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 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)

Recommend

zhengfeng   |   We have carefully selected several similar problems for you:  1532 3572 3416 3081 3491

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int dp[100][100],pre[100];
const int tmin=999999999;
int maxflow;
void EK(int start,int end,int n){
    while(1){
        queue<int>q;
        q.push(1);
       int  minflow=tmin;
        memset(pre,0,sizeof(pre));
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(int i=1;i<=n;i++){
                if(dp[u][i]>0&&!pre[i]){
                    pre[i]=u;
                    q.push(i);
                }
            }
        }
        if(pre[end]==0)
            break;
        for(int i=end;i!=start;i=pre[i]){
            minflow=min(dp[pre[i]][i],minflow);
        }
        for(int i=end;i!=start;i=pre[i]){
            dp[pre[i]][i]-=minflow;
            dp[i][pre[i]]+=minflow;
        }
        maxflow+=minflow;
    }
}
int main(){
   int count=0;
   int n,m;
   int t;
   scanf("%d",&t);
   while(t--){
        scanf("%d%d",&n,&m);
       memset(dp,0,sizeof(dp));
       memset(pre,0,sizeof(pre));
       count++;
       int u,v,w;
       for(int i=1;i<=m;i++){
           scanf("%d%d%d",&u,&v,&w);
           dp[u][v]+=w;
       }
       maxflow=0;
       EK(1,n,n);
       printf("Case %d: %d\n",count,maxflow);
   }
   return 0;
}
时间: 2024-07-31 16:22:58

HDU 3549 基础网络流EK算法 Flow Problem的相关文章

HDU 3549(网络流入门之最大流)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 10327    Accepted Submission(s): 4866 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph,

网络流Ek算法

详细解释待. 紫书模板: struct Edge { int from, to, cap, flow; Edge (int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) { } }; struct EdmondsKarp { int n, m; vector<Edge> edges; vector<int> G[maxn]; int a[maxn]; int p[maxn]; void init(int n) {

HDU 1532 Drainage Ditches EK算法

题意:输入m n, m是边数,n是点数. 接下来m行: 起点,终点,容量.求以 1 为源点, n为汇点的最大流. #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int INF = 0xfffffff; const int MAXN = 200 + 10; //邻接矩阵存放图. int flow[MAXN][

ACM/ICPC 之 ACM计算机工厂-EK算法(POJ3436)

题意有点难读懂 //网络流-EK算法-ACM计算机工厂-构图重点 //Time:0Ms Memory:208K #include <iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; #define MAXN 55 #define INF 0x3f3f3f3f int p,n; int s,t;

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

HDU 3549 Flow Problem (用一道最裸的最大流开启网络流算法之路)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 9423    Accepted Submission(s): 4405 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法

题目链接:HDU 3549 Flow Problem Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted Submission(s): 3824 Problem Description Network flow is a well-known difficult problem f

网络流 HDU 3549 Flow Problem

网络流 HDU 3549 Flow Problem 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法进行求解,注意的问题有两个: 1. 每次增广的时候,反向流量也要进行更行,一开始没注意,WA了几次 ORZ 2. 对于输入的数据,容量要进行累加更新. // 邻接矩阵存储 #include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const i

HDU - 1532 - Drainage Ditches &amp;&amp; 3549 - Flow Problem (网络流初步)

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