BZOJ 4276([ONTAK2015]Bajtman i Okrągły Robin-线段树构图费用流)

普通的费用流,但是点数太多
于是用线段树构图

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (400000+10)
#define MAXM (400000+10)
#define eps (1e-3)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
class Cost_Flow  
{  
public:  
    int n,s,t;  
    int q[1000000];  
    int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;  
    double cost[MAXM];  
    void addedge(int u,int v,int w,double c)    
    {    
        edge[++size]=v;    
        weight[size]=w;    
        cost[size]=c;    
        next[size]=pre[u];    
        pre[u]=size;    
    }    
    void addedge2(int u,int v,int w,double c){addedge(u,v,w,c),addedge(v,u,0,-c);}   
    bool b[MAXN];  
    double d[MAXN];  
    int pr[MAXN],ed[MAXN];  
    bool SPFA(int s,int t)    
    {    
        For(i,n) d[i]=0;  
        MEM(b)  
        d[q[1]=s]=0;b[s]=1;    
        int head=1,tail=1;    
        while (head<=tail)    
        {    
            int now=q[head++];    
            Forp(now)    
            {    
                int &v=edge[p];    
                if (weight[p]&&d[now]+cost[p]>d[v])    
                {    
                    d[v]=d[now]+cost[p];    
                    if (!b[v]) b[v]=1,q[++tail]=v;    
                    pr[v]=now,ed[v]=p;    
                }    
            }    
            b[now]=0;    
        }    
        return fabs(d[t]-0)>eps;    
    }   
    double totcost;    

    double CostFlow(int s,int t)    
    {    
        while (SPFA(s,t))    
        {    
            int flow=INF;    
            for(int x=t;x^s;x=pr[x]) flow=min(flow,weight[ed[x]]);      
            totcost+=(double)flow*d[t];    
            for(int x=t;x^s;x=pr[x]) weight[ed[x]]-=flow,weight[ed[x]^1]+=flow;         
        }    
        return totcost;    
    }    
    void mem(int n,int t)  
    {  
        (*this).n=n;  
        (*this).t=t;  

        size=1;  
        totcost=0;  
        MEM(pre) MEM(next)   
    }  
}S;  
int n,id[MAXN];
int a[MAXN],b[MAXN],c[MAXN];
void build(int o,int L,int R) 
{
    if (L==R) {
        id[L]=o;
        S.addedge2(o,S.t,1,0);
        return;
    }
    int m=(L+R)>>1;
    build(Lson,L,m);S.addedge2(o,Lson,m-L+1,0);
    build(Rson,m+1,R);S.addedge2(o,Rson,R-m,0);
} 
void query(int o,int L,int R,int l,int r,int p) {
    if (l<=L&&R<=r) {
        S.addedge2(p,o,1,0);
        return; 
    }
    int m=(L+R)>>1;
    if (l<=m) query(Lson,L,m,l,r,p);
    if (m<r) query(Rson,m+1,R,l,r,p);
}
int u[MAXN],v[MAXN],w[MAXN];
int main()
{
// freopen("bzoj4276.in","r",stdin);
// freopen(".out","w",stdout);

    cin>>n;
    For(i,n) a[i]=read(),b[i]=read(),c[i]=read();

    int s=30001,t=30002;
    S.mem(t,t);

    build(1,1,5000);


    const int PP=20000;
    For(i,n) {
        S.addedge2(s,PP+i,1,c[i]);
        query(1,1,5000,a[i],b[i]-1,PP+i);
// Fork(j,a[i],b[i]-1) 
// S.addedge2(PP+i,id[j],1,0);
    }
    printf("%0.lf\n",S.CostFlow(s,t)); 


    return 0;
}

你可能感兴趣的:(BZOJ 4276([ONTAK2015]Bajtman i Okrągły Robin-线段树构图费用流))