• Drainage Ditches (HDU


    HDU - 1532

    题意:有m个点,n条管道,问从1到m最大能够同时通过的水量是多少?

    题解:最大流模板题。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    typedef long long ll;
    const int INF = 0x3ffffff;
    const int maxn = 205;
    int gra[maxn][maxn];
    int flow[maxn];
    int path[maxn];
    int n,m;
    
    int bfs(int s, int e)
    {
        queue<int>q;
        int t;
        memset(path,-1,sizeof(path));
        memset(flow,0,sizeof(flow));
        path[s] = 0;
        flow[s] = INF;
        q.push(s);
        while(!q.empty()){
            t = q.front();
            q.pop();
            if(t == e) break;
            for(int i = 1; i <= m; i ++)
            {
                if(i != s && gra[t][i] && path[i] == -1)
                {
                    flow[i] = flow[t] < gra[t][i] ? flow[t] : gra[t][i];
                    path[i] = t;
                    q.push(i);
                }
            }
        }
        if(path[e] == -1)
            return -1;
        else return flow[e];
    }
    
    int EK(int s, int e){
        int maxflow = 0;
        int pre, now, step;
        while((step = bfs(s,e))!= -1){
            maxflow += step;
            now = e;
            while(now != s){
                pre = path[now];
                gra[pre][now] -= step;
                gra[now][pre] += step;
                now = pre;
            }
        }
        return maxflow;
    }
    int main()
    {
    
        int u,v,c;
        while(~scanf("%d%d",&n,&m)){
            memset(gra,0,sizeof(gra));
            for(int i = 1; i <= n; i ++)
            {
                scanf("%d %d %d", &u,&v,&c);
                gra[u][v] += c;
            }
            int ans = EK(1,m);
            printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    LINUX学习笔记day2
    android 获取正在运行的服务
    android小部件
    android开发_国外论坛
    取消线程
    AlarmManager的使用
    PendingIntent
    SharedPreferences 的使用
    流量监听
    android 异常-access to constructor not allowed
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139465.html
Copyright © 2020-2023  润新知