zoukankan      html  css  js  c++  java
  • hdu2883 最大流,判断满流 优化的SAP算法

      这是09年的多校联赛题目,比10年的难度要大。如果没做过hdu3572,建议先去做。有了解题思维再来做这题。

      这题与hdu3572类似。但是1 <= si < ei <= 1,000,000的限制使得我们如果像HDU3572那样建图,会使得边的数量太多。从而超时。

      看看别人的题解 http://www.cnblogs.com/wally/archive/2013/05/03/3057066.html  离散化的思想

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int N=2000,M=1000000, INF=0x3f3f3f3f;
    struct node
    {
        int to,next,w;
    }edge[M];
    int head[N],numh[N],h[N],cure[N],pre[N];
    //numh:GAP优化的统计高度数量数组; h:距离标号数组; cure:当前弧
    int ans,tot;
    void SAP(int s, int e,int n)
    {
        int flow,u,tmp,neck,i;
        ans=0;
        for(i=1;i<=n;i++)
            cure[i]=head[i];
        numh[0]=n;
        u=s;
        while(h[s]<n)
        {
            if(u==e)
            {
                flow =INF;
                for(i=s;i!=e;i=edge[cure[i]].to)
                {
                    if(flow>edge[cure[i]].w)
                    {
                        neck=i;
                        flow =edge[cure[i]].w;
                    }
                }
                for(i=s;i!=e;i=edge[cure[i]].to)
                {
                    tmp=cure[i];
                    edge[tmp].w-=flow;
                    edge[tmp^1].w+=flow;
                }
                ans+=flow;
                u=neck;
            }
            for(i=cure[u];i!=-1;i=edge[i].next)
                if(edge[i].w && h[u]==h[edge[i].to]+1) break;
            if(i!=-1) {cure[u]=i;pre[edge[i].to]=u;u=edge[i].to;}
            else
            {
                if(0==--numh[h[u]]) break; //GAP优化
                cure[u]=head[u];
                for(tmp=n,i=head[u];i!=-1;i=edge[i].next)
                    if(edge[i].w) tmp=min(tmp, h[edge[i].to]);
                h[u]=tmp+1;
                ++numh[h[u]];
                if(u!=s) u=pre[u];
            }
        }
    }
    void init()
    {
        tot=0;
        memset(head,-1,sizeof(head));
        memset(pre,-1,sizeof(pre));
        memset(h,0,sizeof(h));
        memset(numh,0,sizeof(numh));
    
    }
    void addedge(int i,int j,int w)
    {
        edge[tot].to=j;edge[tot].w=w;edge[tot].next=head[i];head[i]=tot++;
        edge[tot].to=i;edge[tot].w=0;edge[tot].next=head[j];head[j]=tot++;
    }
    int T[N],st[N],ed[N];
    int main()
    {
        //freopen("test.txt","r",stdin);
        int n,m,i,j,k,a,b,c,sum,s,t;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            init();
            sum=0;s=n+1;
            k=0;
            for(i=1;i<=n;i++)
            {
                scanf("%d%d%d%d",&st[i],&b,&ed[i],&t);
                sum+=b*t;
                addedge(s,i,b*t);
                T[k++]=st[i];
                T[k++]=ed[i];
            }
            sort(T,T+k);
            j=0;
            for(i=1;i<k;i++)
                if(T[j]!=T[i]) T[++j]=T[i];
            k=s+1+j;
            for(i=1;i<=j;i++)
            {
                addedge(s+i,k,m*(T[i]-T[i-1]));
                for(t=1;t<=n;t++)
                    if(st[t]<=T[i-1]&&ed[t]>=T[i]) addedge(t,i+s,INF);
            }
            SAP(s,k,k);
            if(ans==sum) printf("Yes
    ");
            else printf("No
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    团队冲刺第二阶段2
    团队冲刺第二阶段1
    第十二周总结
    团队游戏
    三个和尚
    第十一周总结
    第十周总结
    调用摄像头拍照
    怎样从相册中选择照片?
    怎样插入并播放音频?
  • 原文地址:https://www.cnblogs.com/Potato-lover/p/3967554.html
Copyright © 2011-2022 走看看