Hdu3549--flow Problem (maximum flow (EK algorithm | | ISAP optimization) _ACM Competition

Source: Internet
Author: User
Topic Source: http://showproblem.php?pid=3549

A network is given to maximize the flow 、。。。 Ideas

There seems to be no idea, the template,,,
Brush three water problems, and finally can go to learn isap ... (Good food ...) Want to cry no tears form ... ) code

#include <cmath> #include <stack> #include <queue> #include <cstdio> #include <cstring> #
Include<algorithm> using namespace std;
const int MAXN=19+10;
const int inf=0x3f3f3f3f;
int n,m;
int PRE[MAXN],F_L[MAXN][MAXN];
BOOL VIS[MAXN];
    void Init () {scanf ("%d%d", &n,&m);
    memset (f_l,0,sizeof (f_l));
        while (m--) {int x,y,w;
        scanf ("%d%d%d", &x,&y,&w);
    F_l[x][y]+=w;
    } bool BFs () {memset (vis,0,sizeof (VIS));
    memset (pre,-1,sizeof (pre));
    Queue<int> Q;
    Q.push (1);
    Vis[1]=1; while (!
        Q.empty ()) {int U=q.front ();
        Q.pop ();
                for (int v=1; v<=n; v++) {if (!vis[v]&&f_l[u][v)) {pre[v]=u;
                Vis[v]=1;
                if (v==n) return 1;
            Q.push (v);
}} return 0;
    } void Print_ek () {int maxflow=0; while (BFS ()) {int tt=n,minN=inf;
            while (tt!=1) {minn=min (F_l[pre[tt]][tt],minn);
        TT=PRE[TT];
        } Maxflow+=minn;
        Tt=n;
            while (tt!=1) {F_l[pre[tt]][tt]-=minn;
            F_l[tt][pre[tt]]+=minn;
        TT=PRE[TT];
} printf ("%d\n", Maxflow);
    int main () {int T;
    scanf ("%d", &t);
        for (int i=1;i<=t;i++) {init ();
        printf ("Case%d:", i);
    Print_ek ();
return 0;

 }
ISAP optimization #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using
namespace Std;
const int inf=0x3f3f3f3f;
const int MAXN=1000+10;
Use elevation, look for the path, and put the edge of the ordinal record in the array, found, to operate.
    struct PP {int v,nexx;
int flow,cap;
}edge[maxn<<1];
int FIRST[MAXN],H[MAXN],NUM[MAXN],CUR[MAXN],S[MAXN];
ISAP: adjacency table + array record int n,m,tot;
    void ADD (int u,int v,int W) {edge[tot].v=v;
    edge[tot].flow=0;
    Edge[tot].cap=w;
    Edge[tot].nexx=first[u];
first[u]=tot++;
    } void init (int i) {tot=0;
    scanf ("%d%d", &n,&m);
    memset (First,-1,sizeof (a));
        while (m--) {int u,v,w;
        scanf ("%d%d%d", &u,&v,&w);
        ADD (U,V,W);
    ADD (v,u,0);
printf ("Case%d:", i);}
    void height () {queue<int> Q; while (!
    Q.empty ()) Q.pop ();
    memset (h,-1,sizeof (h));
    memset (num,0,sizeof (num));
    Q.push (n);
    h[n]=0;
    ++NUM[0]; while (!
        Q.empty ()) {int U=q.front (); Q.poP ();
            for (int i=first[u];i!=-1;i=edge[i].nexx) {int v=edge[i].v;
                if (h[v]==-1) {h[v]=h[u]+1;
                Q.push (v);
            num[h[v]]++;
    }} void Print_isap (int s,int t) {height ();
    int ans=0,top=0,u=s;
    memcpy (Cur,first,sizeof (a));
            while (h[s]<n) {if (u==t) {int d=inf,pos;
                    for (int i=0;i<top;i++) {if (D>edge[s[i]].cap-edge[s[i]].flow) {
                    D=edge[s[i]].cap-edge[s[i]].flow;
                Pos=i;
                } for (int i=0;i<top;i++) {edge[s[i]].flow+=d;
            Edge[s[i]^1].flow-=d;
            } Top=pos;
            Ans+=d;
        u=edge[s[top]^1].v;//back point continue; The int i=cur[u];//starts with the next for (; I!=-1;i=edge[i].nexx) {
            if (edge[i].cap>edge[i].flow&&h[u]==h[edge[i].v]+1) {cur[u]=i;
                S[top++]=i;
                U=EDGE[I].V;
            Break } if (i==-1) {int d=n;//fallback, mark height for (int I=first[u];i!=-1;i=edge[i].nex
                    x) {if (edge[i].cap>edge[i].flow&&d>h[edge[i].v]) {
                    D=H[EDGE[I].V];
                Cur[u]=i; } if (--num[h[u]]==0) break;//If there is only one point at the current height, the fault will occur after the change.
            Exit ++num[h[u]=d+1];
if (u!=s) u=edge[s[--top]^1].v;//back to previous} printf ("%d\n", ans);
    int main () {int T;
    scanf ("%d", &t);
        for (int i=1;i<=t;i++) {init (i);
    PRINT_ISAP (1,n);
 }
}

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.