HDU4280 Island Transport "Max Stream" "SAP"

Source: Internet
Author: User
Tags min

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=4280


Main topic:

There are N Islands, M two Way road. Each route can be up to CI individuals per hour. Give you the coordinates of the N islands. Q: Within one hours,

How many visitors can be sent from the westernmost island to the most eastern island.


Ideas:

Network flow to find the maximum flow of the bare topic. Locate the westernmost island and the easternmost island by coordinates, and record and mark as source and sink points. And then

Using the chain forward star to store the diagram, the M-bar bidirectional Edge is added to the diagram. Then use the SAP algorithm to do, it is said that there is no card SAP network stream.

The algorithm uses Gap optimization, current arc optimization, specific reference code.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <
Queue> using namespace std;        const int MAXN = 100010;
Maximum number of points const int MAXM = MAXN*4;

const int INF = 0XFFFFFF0;
    struct Edgenode {int to;
    int next;
int W;
}EDGES[MAXM];                  int head[maxn],id;
    Chain-forward star void addedges (int u,int v,int w) {edges[id].to = v;
    EDGES[ID].W = W;
    Edges[id].next = Head[u];
    Head[u] = id++;
    edges[id].to = u;
    EDGES[ID].W = 0;
    Edges[id].next = Head[v];
HEAD[V] = id++;
} int NUMH[MAXN],H[MAXN],CUREDGES[MAXN],PRE[MAXN];
NUMH: An array of statistical height numbers for gap optimization;
H: array of distance labels;
    Curedges: Current arc array//pre: precursor array void BFS (int end,int N)//bfs find the distance label value for each point {memset (numh,0,sizeof (NUMH));
    for (int i = 1; I <= N; ++i) numh[h[i]=n]++;
    H[end] = 0;
    numh[n]--;
    numh[0]++;
    Queue<int> Q;

    Q.push (end); while (!
        Q.empty ()) {int v = q.front ();
        Q.pop (); int i = HeaD[V];

            while (i! =-1) {int u = edges[i].to;
                if (H[u] < N) {i = Edges[i].next;
            Continue
            } H[u] = H[v] + 1;
            numh[n]--;
            numh[h[u]]++;
            Q.push (U);
        i = Edges[i].next;  }}} int sapmaxflow (int start,int end,int N) {int curflow,flowans = 0,temp,neck;
    Flowans: Maximum flow, initialized to 0 memset (h,0,sizeof (h));
    memset (pre,-1,sizeof (pre));          for (int i = 1; I <= N; ++i) curedges[i] = Head[i];
    Initializes the current arc to the first adjacency edge//numh[0] = N;
    BFS (End,n);
    int u = start; while (H[start] < n)//when H[start] >= N, the gap {if (U = = end) {Curflow) must appear in the network
            = INF; for (int i = start; I! = end; i = edges[curedges[i]].to) {if (Curflow > EDGES[CUREDGES[I]].W
                    ) {neck = i; Curflow = Edges[CUREDGES[I]].W;
                }}//augmented successfully, looking for "bottleneck" side for (int i = start; I! = end; i = edges[curedges[i]].to) {
                temp = Curedges[i];
                EDGES[TEMP].W-= Curflow;
            EDGES[TEMP^1].W + = Curflow;
            }//Modify the edge capacity on the path Flowans + = Curflow;   u = neck;
        Next augmentation starts with the bottleneck} int i;
                for (i = curedges[u]; i =-1; i = edges[i].next) if (edges[i].w && h[u] = = h[edges[i].to]+1)  Break
            Looking for a workable arc if (i! =-1)//gap optimization {Curedges[u] = i;
            Pre[edges[i].to] = u;
        u = edges[i].to;
            } else {if (0 = =--numh[h[u]]) break;
            Curedges[u] = Head[u]; for (temp = N,i = Head[u]; I! =-1; i = edges[i].next) if (EDGES[I].W) temp = min (temp,h
            [Edges[i].to]);
            H[u] = temp + 1; ++numH[h[u]];
        if (u! = start)//re-label and re-augment u = Pre[u] from the current point precursor;
}} return Flowans;
    } int main () {int t,n,m,u,v,w;
    int start,end;
    scanf ("%d", &t);
        while (t--) {int Max =-inf,min = INF;
        scanf ("%d%d", &n,&m);
            for (int i = 1; I <= N; ++i)//Find the westernmost and easternmost islands, record source and sink points {scanf ("%d%d", &u,&v);
                if (u <= min) {min = u;
            start = i;
                } if (U >= max) {max = u;
            end = i;
        }} memset (Head,-1,sizeof (Head));
        id = 0;
            for (int i = 0; i < M; ++i)//Add Edge {scanf ("%d%d%d", &u,&v,&w);
            Addedges (U,V,W);
        Addedges (V,U,W); 
    } printf ("%d\n", Sapmaxflow (Start,end,n));
} 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.