Codevs 1993 Grassland Drainage [dinic] [Maximum flow]

Source: Internet
Author: User

1993 Grassland Drainage Usaco
Time limit: 2 s
Space limit: 256000 KB
Title Level: Diamonds Diamond

Title Description Description
On Farmer John's farm, whenever it rains, Bessie's favorite clover accumulates a pond of water. This means that the grass is submerged, and the grass will continue to grow for a long time. As a result, farmer John built a drainage system to relieve Bessie's meadows of the trouble of flooding (don't worry, the rain will flow to a nearby stream). As a first-class technician, farmer John has a controller at one end of each gutter so he can control the amount of water flowing into the gutter.

Farmer John knows the amount of water that can flow every minute of each drain, and the exact layout of the drainage system (a net from the pool to the end of the creek). It is important to note that sometimes there is not only one gutter from one place to another.

Based on this information, the maximum flow from the pool drain to the creek is calculated. For each of the given drains, rainwater can only flow in one direction, noting the possibility of a circular flow of rainwater.

Enter a description input Description
Line 1th: Two integers separated by a space n (0 <= n <= 200) and M (2 <= m <= 200). n is the number of drains that the farmer John has dug, and M is the number of drains crossing points. Intersection 1 is a puddle, and the intersection M is a brook.

Line two to n+1: each line has three integers, Si, Ei, and Ci. Si and ei (1 <= si, ei <= M) indicate intersections at both ends of the gutter, and rainwater flows from Si to Ei. CI (0 <= ci <= 10,000,000) is the maximum capacity of this gutter.

Outputs description Output Description
Outputs an integer, which is the maximum flow of drainage.

Sample input to sample
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample output Sample Outputs
50

Dinic naked question.

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <set> #
include<string> #include <iomanip> #include <ctime> #include <climits> #include <cctype>
#include <algorithm> #ifdef WIN32 #define AUTO "%i64d" #else #define AUTO "%lld" #endif using namespace std; #define SMAX (X,TMP) X=max (x), (TMP) #define SMIN (x,tmp) X=min ((x), (TMP)) #define MAXX (X1,X2,X3) Max (Max (X1,X2), x3) #
Define MINN (x1,x2,x3) min (min (x1,x2), x3) const int inf=0x3f3f3f3f;
const int MAXN = 205;
    struct Edge {int to,next;
int cap;
}edge[maxn<<1];
int HEAD[MAXN];
int Maxedge;
    inline void Addedge (int u,int v,int c) {Edge[++maxedge] = (edge) {v,head[u],c};
    Head[u] = Maxedge;
    Edge[++maxedge] = (Edge) {u,head[v],0};
HEAD[V] = Maxedge;
} int n,m;
int s,t; void Init () {scanf ("%d%d", &m,&n);
    memset (head,-1,sizeof (head));
    Maxedge=-1;
        for (int i=1;i<=m;i++) {int x,y,c;
        scanf ("%d%d%d", &x,&y,&c);
    Addedge (X,Y,C);
} s=1,t=n;
} int DIS[MAXN],CUR[MAXN];
    BOOL BFS (int s,int T) {queue <int> que;
    memset (dis,0,sizeof (dis)); Que.push (S); Dis[s]=1;
    Initialize as 1 at the first to avoid failed judgement of visited!!!
        while (!que.empty ()) {int U=que.front (); Que.pop ();
        if (u==t) return true;
            for (int i=head[u];~i;i=edge[i].next) {int v = edge[i].to;
            if (Dis[v] | |!edge[i].cap) continue;
            DIS[V] = Dis[u] + 1;
        Que.push (v);
}} return false;
    } int dfs (int u,int t,int minflow) {if (u==t | |!minflow) return minflow;
    int flow=0;
        for (int &i=cur[u];~i;i=edge[i].next) {int v=edge[i].to,f; if (Dis[u]+1==dis[v] && (F=dfs (V,t,min (MINFLOW,EDGE[I].CAP))) edge[i].cap-=f,edge[i^1].cap+=f,flow+=f,minflow-=f;
    if (!minflow) break;
} return flow;
    } int dinic (int s,int T) {int ret=0;
        while (BFS (s,t)) {memcpy (cur,head,sizeof (head));
    RET + = DFS (s,t,inf);
} return ret;
    } int main () {freopen ("grass.in", "R", stdin);
    Freopen ("Grass.out", "w", stdout);
    Init ();
    printf ("%d", Dinic (s,t));
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.