zoukankan      html  css  js  c++  java
  • Drainage Ditches HDU

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
    Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
    Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

    InputThe input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
    OutputFor each case, output a single integer, the maximum rate at which water may emptied from the pond.
    Sample Input

    5 4
    1 2 40
    1 4 20
    2 4 20
    2 3 30
    3 4 10

    Sample Output

    50
    #include <bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<set>
    #include<vector>
    #include<iomanip>
    using namespace std;
    typedef long long ll;
    const int inf = 0x3f3f3f3f;
    const int mod = 7;
    const double eps = 1e-8;
    const int mx = 3e2; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
    bool isprime(int n) { if (n <= 1)return 0; for (int i = 2; i * i <= n; i++)if (n % i == 0)return 0; return 1; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(ll i=(a),_=(b);i>=_;i--)
    #define clr(a,b) memset(a, b, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    inline ll qpow(ll a, ll b) { return b ? ((b & 1) ? a * qpow(a * a % mod, b >> 1) % mod : qpow(a * a % mod, b >> 1)) % mod : 1; }
    //inline ll qpow(ll a, ll b, ll c) { return b ? ((b & 1) ? a * qpow(a * a % c, b >> 1) % c : qpow(a * a % c, b >> 1)) % c : 1; }
    void ca(int kase, int ans) { cout << "Case #" << kase << ": " << ans << endl; }
    void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    int n, m, t, k;
    const int NUM = 105;
    int graph[mx][mx],pre[mx];//邻接矩阵存图
    int bfs(int s, int t) {
        int flow[mx];
        clr(pre, -1);
        flow[s] = inf;
        queue<int>Q;
        Q.push(s);
        while (!Q.empty())
        {
            int u = Q.front(); Q.pop();
            if (u == t)break;
            re(i, 1, m + 1) {
                if (i != s && graph[u][i] && pre[i] == -1) {
                    pre[i] = u;
                    Q.push(i);
                    flow[i] = min(flow[u], graph[u][i]);
                }
            }
        }
        if (pre[t] == -1)return -1;
        return flow[t];
    }
    int maxFlow(int s, int t) {
        int Maxflow = 0;
        while (1)
        {
            int flow = bfs(s, t);
            if (flow == -1)break;
            int cur = t;
            while (cur != s) {
                int father = pre[cur];
                graph[father][cur] -= flow;
                graph[cur][father] += flow;
                cur = father;
            }
            Maxflow += flow;
        }
        return Maxflow;
    }
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        while (~scanf("%d%d",&n,&m))
        {
            clr(graph, 0);
            re(i, 0, n) {
                int u, v, w;
                sc(u), sc(v), sc(w);
                graph[u][v] += w;
            }
            cout << maxFlow(1, m) << endl;
        }
        return 0;
    }
  • 相关阅读:
    Nosql介绍
    linux系统安装mysql
    linux下装tomcat教程
    linux系统下安装jdk教程
    XML解析
    XML 可扩展标记语言
    Oracle——子查询
    数据库——Oracle(增删改查,单行函数,多行函数,多表查询)
    Ubuntu16.04Scrapy爬虫定时任务
    大数据——zookeeper分布式集群管理工具的学习与使用
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12918341.html
Copyright © 2011-2022 走看看