hdu - 3549 Flow Problem (最大流模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=3549

Ford-Fulkerson算法.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std;

struct edge
{
    int to,cap,rev;
    edge(){}
    edge(int x,int y,int z)
    {
        to=x;
        cap=y;
        rev=z;
    }
};

vector<edge>G[maxv];
bool used[maxv];

void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

int dfs(int v,int t,int f)
{
    if(v==t) return f;
    used[v]=true;
    for(int i=0;i<G[v].size();i++)
    {
        edge &e=G[v][i];
        if(!used[e.to]&&e.cap>0)
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
             //   printf("%d %d\n",e.to,e.rev);
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        memset(used,0,sizeof(used));
        int f=dfs(s,t,inf);
        if(f==0) return flow;
        flow+=f;
    }
}
int main()
{
    //freopen("a.txt","r",stdin);
    int t,n,m,a,b,c,j=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
            G[i].clear();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
        }
        printf("Case %d: %d\n",j++,max_flow(1,n));
    }
    return 0;
}

Dinic算法:(动态邻接表)

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <vector>
  5 #include <cstring>
  6 #include <string>
  7 #include <algorithm>
  8 #include <string>
  9 #include <set>
 10 #include <functional>
 11 #include <numeric>
 12 #include <sstream>
 13 #include <stack>
 14 #include <map>
 15 #include <queue>
 16 #pragma comment(linker, "/STACK:102400000,102400000")
 17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
 18
 19 #define ll long long
 20 #define inf 0x7f7f7f7f
 21 #define lc l,m,rt<<1
 22 #define rc m + 1,r,rt<<1|1
 23 #define pi acos(-1.0)
 24
 25 #define L(x)    (x) << 1
 26 #define R(x)    (x) << 1 | 1
 27 #define MID(l, r)   (l + r) >> 1
 28 #define Min(x, y)   (x) < (y) ? (x) : (y)
 29 #define Max(x, y)   (x) < (y) ? (y) : (x)
 30 #define E(x)        (1 << (x))
 31 #define iabs(x)     (x) < 0 ? -(x) : (x)
 32 #define OUT(x)  printf("%I64d\n", x)
 33 #define lowbit(x)   (x)&(-x)
 34 #define Read()  freopen("a.txt", "r", stdin)
 35 #define Write() freopen("b.txt", "w", stdout);
 36 #define maxn 1010
 37 #define maxv 1010
 38 #define mod 1000000000
 39 using namespace std;
 40
 41 struct edge
 42 {
 43     int to,cap,rev;
 44     edge(){}
 45     edge(int x,int y,int z)
 46     {
 47         to=x;
 48         cap=y;
 49         rev=z;
 50     }
 51 };
 52
 53 vector<edge>G[maxv];
 54 int level[maxv];
 55 int iter[maxv];
 56
 57 void add_edge(int from,int to,int cap)
 58 {
 59     G[from].push_back((edge){to,cap,G[to].size()});
 60     G[to].push_back((edge){from,0,G[from].size()-1});
 61 }
 62
 63 void bfs(int s)
 64 {
 65     memset(level,-1,sizeof(level));
 66     queue<int>que;
 67     level[s]=0;
 68     que.push(s);
 69     while(!que.empty())
 70     {
 71         int v=que.front();que.pop();
 72         for(int i=0;i<G[v].size();i++)
 73         {
 74             edge &e=G[v][i];
 75             if(e.cap>0&&level[e.to]<0)
 76             {
 77                 level[e.to]=level[v]+1;
 78                 que.push(e.to);
 79             }
 80         }
 81     }
 82 }
 83 int dfs(int v,int t,int f)
 84 {
 85     if(v==t) return f;
 86     for(int &i=iter[v];i<G[v].size();i++)
 87     {
 88         edge &e=G[v][i];
 89         if(e.cap>0&&level[v]<level[e.to])
 90         {
 91             int d=dfs(e.to,t,min(f,e.cap));
 92             if(d>0)
 93             {
 94                 e.cap-=d;
 95                 G[e.to][e.rev].cap+=d;
 96               //  printf("%d %d %d\n",e.to,e.rev,G[e.to][e.rev]);
 97                 return d;
 98             }
 99         }
100     }
101     return 0;
102 }
103
104 int max_flow(int s,int t)
105 {
106     int flow=0;
107     for(;;)
108     {
109         bfs(s);
110         if(level[t]<0) return flow;
111         memset(iter,0,sizeof(iter));
112         int f;
113         while((f=dfs(s,t,inf))>0)
114         flow+=f;
115     }
116 }
117 int main()
118 {
119     //freopen("a.txt","r",stdin);
120     int t,n,m,a,b,c,j=1;
121     scanf("%d",&t);
122     while(t--)
123     {
124         scanf("%d%d",&n,&m);
125         for(int i=0;i<=n;i++)
126             G[i].clear();
127         for(int i=0;i<m;i++)
128         {
129             scanf("%d%d%d",&a,&b,&c);
130             add_edge(a,b,c);
131         }
132         printf("Case %d: %d\n",j++,max_flow(1,n));
133     }
134     return 0;
135 }
时间: 2024-10-26 08:29:54

hdu - 3549 Flow Problem (最大流模板题)的相关文章

HDU 3549 Flow Problem(网络流模板题)

记录一下模板 #include <vector> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> using namespace std; #define maxn 1100 #define INF 0x7f7f7f7f struct Edge { int from,to,cap,flow; }; struct Dinic { int n,m,s

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 (最大流入门题)

增广路: 1 /************************************************************* 2 题目: Flow Problem(HDU 3549) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 4 题意: 给一个单向图,求从1到n的最大流 5 算法: 最大流之增广路(入门) 6 算法思想: 不断用BFS找通路,没每找一条路,记录这条路的最小流, 7 再给这条路上的所有流量减去这个最小值.

HDU 3549 Flow Problem (最大流)

链接:click here 题意: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. 翻译:网络流量是一个众所周知的难题ACMers.给定一个图,你的任务是找出加权有向图的最大流. 输出格式: Case 1: 1 Case 2: 2 思路:跟hdu153

hdu 3549 Flow Problem(最大流模板题)

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

hdu 3549 Flow Problem (网络最大流)

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

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

[ACM] hdu 3549 Flow Problem (最大流模板题)

Flow Problem 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 nu