• [USACO4.2] 草地排水 Drainage Ditches (最大流)


    题目背景
    在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水。这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间。因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪)。作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量。

    题目描述
    农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网)。需要注意的是,有些时候从一处到另一处不只有一条排水沟。

    根据这些信息,计算从水潭排水到小溪的最大流量。对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形。

    输入输出格式
    输入格式:
    第1行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200)。N是农夫John已经挖好的排水沟的数量,M是排水沟交叉点的数量。交点1是水潭,交点M是小溪。

    第二行到第N+1行: 每行有三个整数,Si, Ei, 和 Ci。Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei。Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量。

    输出格式:
    输出一个整数,即排水的最大流量。

    输入输出样例
    输入样例#1:
    5 4
    1 2 40
    1 4 20
    2 4 20
    2 3 30
    3 4 10
    输出样例#1:
    50
    说明
    题目翻译来自NOCOW。

    USACO Training Section 4.2

    code:

    //Menteur_Hxy
    #include <cstdio>
    #include <iostream>
    #include <queue>
    #include <cstring>
    #define ll long long
    #define f(a,b,c) for(int a=b;a<=c;a++)
    using namespace std;
    
    ll rd() {
        ll x=0,fla=1; char c=' ';
        while(c<'0' || c>'9') {c=getchar();if(c=='-') fla=-fla;};
        while(c>='0' && c<='9') x=x*10+c-'0',c=getchar();
        return x*fla;
    }
    
    
    const int INF=0x3f3f3f3f;
    const int MAX=501;
    const int T=201;
    int n,m,ans,cnt=1;
    int h[MAX],head[MAX],cur[MAX];
    
    struct edges {
        int next,to,fl;
    }edge[MAX<<1];
    
    void add(int c,int b,int a) {
        //下面的三个rd()会倒序执行,查了半天qaq
        // cout<<a<<"-"<<b<<"-"<<c<<endl;
        edge[++cnt]=(edges){head[a],b,c}; head[a]=cnt;
        edge[++cnt]=(edges){head[b],a,0}; head[b]=cnt;
    }
    
    queue <int> q;
    
    bool bfs() {
        memset(h,-1,sizeof h);
        h[1]=0;
        q.push(1);
        while(!q.empty()) {
            int u=q.front(); q.pop();
            // cout<<u<<endl;
            // cout<<head[u]<<endl;
            for(int i=head[u];i;i=edge[i].next) {
                if(edge[i].fl && h[edge[i].to]==-1) {
                    h[edge[i].to]=h[u]+1;
                    q.push(edge[i].to);
                }
            }
        }
        if(h[n]==-1) return 0;
        else return 1;
    }
    
    int dfs(int x,int f) {
        if(x==n) return f;
        int w,used=0;
        for(int i=head[x];i;i=edge[i].next)
                if(edge[i].fl && h[edge[i].to]==h[x]+1) {
                    w=dfs(edge[i].to,min(f-used,edge[i].fl));
                    used+=w; edge[i].fl-=w; edge[i^1].fl+=w;
                    if(used==f) return f;
                }
        // if(!used) h[n]=-1;
        return used;
    }
    
    void dinic() {
        while(bfs()) ans+=dfs(1,INF);
    }
    
    int main() {
        m=rd(),n=rd();
        f(i,1,m) add(rd(),rd(),rd());
        // f(i,1,n) cout<<head[i]<<" ";
        // cout<<endl;
        dinic();
        printf("%d",ans);
        return 0;
    }
    版权声明:本文为博主原创文章,未经博主允许不得转载。 博主:https://www.cnblogs.com/Menteur-Hxy/
  • 相关阅读:
    JavaScript的由来, 浏览器的20年
    WEB界面onload前的加载流程❤❤
    HTML5文件系统API和资料整理
    No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type LeadRestControllerTest (e.g. x.new A() where x is an instance of ).
    Git Gerrit Code Review
    Nginx Configuring HTTPS servers
    自签名证书 nginx tomcat
    centos yum install nginx
    局域网 服务器 https
    分布式重复提交
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9247978.html
Copyright © 2020-2023  润新知