您的位置:首页 > 其它

BZOJ 4276 ONTAK2015 Bajtman i Okrągły Robin 费用流+线段树优化构图

2015-09-28 15:52 519 查看
题目大意:给定n个带权区间和1W个点,求最大权匹配

考虑直接连边,边数为O(n2)O(n^2)级别,难以承受

线段树优化构图,边数O(nlogn)O(nlogn)

方法是建一棵线段树,父亲节点向子节点连流量为INF边权为0的边

然后一个区间向对应的loglog个节点连边即可

然而慢得没进排行榜233

标解是啥QwQ

[code]#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 45010
#define S (M-1)
#define T (M-2)
#define INF 0x3f3f3f3f
using namespace std;

int n,ans;
int X[M],Y[M],Z[M];

namespace Max_Cost_Flow{
    struct abcd{
        int to,flow,cost,next;
    }table[2002002];
    int head[M],tot=1;
    void Add(int x,int y,int f,int c)
    {
        table[++tot].to=y;
        table[tot].flow=f;
        table[tot].cost=c;
        table[tot].next=head[x];
        head[x]=tot;
    }
    void Link(int x,int y,int f,int c)
    {
        Add(x,y,f,c);
        Add(y,x,0,-c);
    }
    bool Edmonds_Karp()
    {
        static int q[65540],flow[M],cost[M],from[M];
        static unsigned short r,h;
        static bool v[M];
        int i;
        memset(cost,0xef,sizeof cost);
        flow[S]=1;cost[S]=0;q[++r]=S;
        while(r!=h)
        {
            int x=q[++h];v[x]=false;
            for(i=head[x];i;i=table[i].next)
                if(table[i].flow&&cost[table[i].to]<cost[x]+table[i].cost)
                {
                    cost[table[i].to]=cost[x]+table[i].cost;
                    flow[table[i].to]=1;
                    from[table[i].to]=i;
                    if(!v[table[i].to])
                        v[table[i].to]=true,q[++r]=table[i].to;
                }
        }
        if(cost[T]<=0) return false;
        ans+=cost[T];
        for(i=from[T];i;i=from[table[i^1].to])
            table[i].flow--,table[i^1].flow++;
        return true;
    }
}

void Initialize(int p,int x,int y)
{
    int mid=x+y>>1;
    if(x==y)
    {
        Max_Cost_Flow::Link(p,T,1,0);
        return ;
    }
    Max_Cost_Flow::Link(p,p<<1,INF,0);
    Max_Cost_Flow::Link(p,p<<1|1,INF,0);
    Initialize(p<<1,x,mid);
    Initialize(p<<1|1,mid+1,y);
}

void Link(int p,int x,int y,int l,int r,int from)
{
    int mid=x+y>>1;
    if(x==l&&y==r)
    {
        Max_Cost_Flow::Link(from,p,INF,0);
        return ;
    }
    if(r<=mid)
        Link(p<<1,x,mid,l,r,from);
    else if(l>mid)
        Link(p<<1|1,mid+1,y,l,r,from);
    else
        Link(p<<1,x,mid,l,mid,from) , Link(p<<1|1,mid+1,y,mid+1,r,from) ;
}

int main()
{
    int i,_max=0,_min=10000;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf("%d%d%d",&X[i],&Y[i],&Z[i]);--Y[i];
        _max=max(_max,Y[i]);
        _min=min(_min,X[i]);
    }
    Initialize(1,_min,_max);
    for(i=1;i<=n;i++)
    {
        Link(1,_min,_max,X[i],Y[i],40000+i);
        Max_Cost_Flow::Link(S,40000+i,1,Z[i]);
    }
    while( Max_Cost_Flow::Edmonds_Karp() );
    cout<<ans<<endl;
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: