Network Flow (iii)----maximum stream SAP algorithm

Source: Internet
Author: User

Template with HDU 3572 Task Schedule as an example

Code:

#include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath > #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map >using namespace Std;const int inf = 0x3f;const int inf = 0x3f3f3f3f;const int MAXN = 40002;struct node{int to, Flo W, Next;} EDGE[MAXN * 8];int N, m;int S, T;int tot;int q[maxn];int DIS[MAXN], PRE[MAXN], REC[MAXN], Head[maxn];int GAP[MAXN], Cur[ma    xn];inline void Add_edge (int a, int b, int c) {edge[tot].to = b; edge[tot].flow = c; edge[tot].next = Head[a];    Head[a] = tot++; Edge[tot].to = A; Edge[tot].flow = 0;    Edge[tot].next = Head[b]; HEAD[B] = tot++;}     void Init () {tot = 0; Memset (Head,-1, sizeof (head));}     void BFS () {int i;    int h = 0, t = 0;    for (i = 0; I <= T; i++) dis[i] = INF;    Q[H] = T;    Dis[t] = 0;        while (H <= t) {int u = q[h++];        gap[dis[u]]++;    for (i = head[u]; I! =-1; i = edge[i].next)    {int v = edge[i].to;                if (dis[v] = = INF && edge[i^1].flow) {Dis[v] = Dis[u] + 1;            Q[++t] = v;    }}}}int SAP () {int i;    int u = pre[s] = S, Maxflow = 0, flow = INF;        for (i = 0; I <= t; i++)//Here T is the maximum vertex number {Gap[i] = 0;    Cur[i] = Head[i];    } BFS ();            while (Dis[s] <= t) {if (U = = t) {maxflow + = flow;                for (; U! = S; u = pre[u]) {edge[rec[u]].flow-= flow;            Edge[rec[u]^1].flow + = flow;        } flow = INF;            } for (i = cur[u]; I! =-1; i = edge[i].next) {int v = edge[i].to;                if (Edge[i].flow && dis[u] = = Dis[v] + 1) {flow = min (flow, edge[i].flow);                PRE[V] = u;                REC[V] = i;                Cur[u] = i;//current arc optimized u = V;            Break        }} if (i = =-1) {int Mindis = T + 1;//t is vertex number if ((--gap[dis[u]) = = 0) break;//clearance optimization for (i = cur[u] = Head[u]; I! =-1; i = edge[i].next) {if (Edge[i].flow && Mindis & Gt                Dis[edge[i].to]) {Mindis = dis[edge[i].to];            }} Gap[dis[u] = mindis+1]++;        U = pre[u]; }} return maxflow;}    int main () {int ncase,n,m;    scanf ("%d", &ncase);        for (int kase = 1;kase <= ncase;kase++) {Init ();        scanf ("%d%d", &n,&m);        S = 0,t = 1001;        int sum = 0;            for (int i = 1;i <= n;i++) {int start,process,end;            scanf ("%d%d%d", &process,&start,&end);            sum + = process;            Add_edge (s,i,process);        for (int j = start;j <= end;j++) Add_edge (i,500+j,1); } for (int i = 501;i <= 1000;i++) Add_edge (i,t,m);       BOOL flag = TRUE;        int tmp = SAP ();        printf ("TMP =%d\n", TMP);        if (tmp! = SUM) flag = false;        printf ("Case%d:", Kase);        if (flag) printf ("yes\n\n");    else printf ("no\n\n"); } return 0;}

  

Network Flow (iii)----maximum stream SAP algorithm

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.