题解:P7425 [THUPC2017] 机场

· · 题解

首先,无解的情况可以做一遍差分判掉。

有解的情况考虑图论建模,构建一个由时间和飞机构成的网络。

我们把源点看作天空,汇点也看作天空(?),飞机 i 从天空飞来,相当于一条 (s \to i,1,0) 的边,飞机 i 飞向天空,相对于一条 (I \to t,1,0) 的边。(I 是飞走的时间,ii 是飞来的时间,后同)

每个时间只能有 a 个飞机在登机桥区,相当于一条 (time \to time+1,1,a) 的边。

一个飞机不可能从摆渡车区到登机桥区,只有可能是从登机桥区到摆渡车区,由于有解,我们对这一种飞机的处理就是让它把所有乘客接着然后直接飞走,相当于一条 (ii+1 \to i,1,sz_i \times p) 的边。

其余的情况就只有一直在登机桥区:一条 (I \to i,1,0) 的边;一直在摆渡车区:一条 (ii \to i,1,sz_i)。

再跑一遍费用流就可以了。

#include<bits/stdc++.h>
using namespace std;
#define int long long 

const int N=2e5+5;
class eg
{
public:
    int to,w,val;
    int unsigned ip;
};
int s,t,n,dj,bd,a[N],tot,sz[N],st[N],ed[N],dis[N],hig[N],diff[N],sum[N];
double p;
vector<eg>nbr[N];
bool vis[N];

void add(int x,int y,int w,int val)
{
    nbr[x].push_back({y,w,val,nbr[y].size()});
    nbr[y].push_back({x,0,-val,nbr[x].size()-1ull});
    return ;
}

void lish()
{
    sort(a+1,a+tot+1);
    tot=unique(a+1,a+tot+1)-(a+1);
    for(int i=1;i<=n;i++)
    {
        st[i]=lower_bound(a+1,a+tot+1,st[i])-a;
        ed[i]=lower_bound(a+1,a+tot+1,ed[i])-a;
    }
    return ;
}

bool bfs(int st)
{
    queue<int>q;
    q.push(st);
    for(int i=s;i<=t;i++)
        dis[i]=1e18,hig[i]=0,vis[i]=false;
    dis[st]=0;
    vis[st]=true;
    while(q.empty()==false)
    {
        int cur=q.front();
        q.pop();
        vis[cur]=false;
        for(auto dat:nbr[cur])
        {
            int nxt=dat.to,w=dat.w,val=dat.val;
            if(dis[cur]+val<dis[nxt]&&w>0)
            {
                dis[nxt]=dis[cur]+val;
                if(vis[nxt]==false)
                {
                    q.push(nxt);
                    vis[nxt]=true;
                }
            }
        }
    }
    return dis[t]<=1e12;
}

pair<int,int>dfs(int cur,int now)
{
    if(cur==t)
        return {now,0};
    int num=0,fy=0;
    for(int i=hig[cur];i<nbr[cur].size();i=hig[cur])
    {
        hig[cur]=i+1;
        int nxt=nbr[cur][i].to,w=nbr[cur][i].w,ip=nbr[cur][i].ip,val=nbr[cur][i].val;
        if(dis[cur]+val==dis[nxt]&&w>0)
        {
            auto h=dfs(nxt,min(now,w));
            num+=h.first;
            fy+=h.second+h.first*val;
            now-=h.first;
            nbr[cur][i].w-=h.first;
            nbr[nxt][ip].w+=h.first;
            if(now<=0)
                break;
        }
    }
    return {num,fy};
}

void Work()
{
    cin>>n>>dj>>bd>>p;
    for(int i=0;i<=2e5;i++)
        nbr[i].clear(),diff[i]=0;
    tot=0;
    for(int i=1;i<=n;i++)
    {
        cin>>sz[i]>>st[i]>>ed[i];
        a[++tot]=st[i];
        a[++tot]=ed[i];
    }
    lish();
    for(int i=1;i<tot;i++)
        add(i,i+1,dj,0);
    s=0,t=tot+n+1;
    for(int i=1;i<=n;i++)
    {
        diff[st[i]]++;
        diff[ed[i]]--;
        add(s,st[i],1,0);
        add(i+tot,t,1,0);
        add(st[i],i+tot,1,sz[i]);
        add(ed[i],i+tot,1,0);
        add(st[i]+1,i+tot,1,floor(sz[i]*p+1e-6));
    }
    for(int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]+diff[i];
        if(sum[i]>dj+bd)
        {
            puts("impossible");
            return ;
        }
    }
    int ans=0,fy=0;
    while(bfs(0)==true)
    {
        auto now=dfs(0,1e18);
        ans+=now.first;
        fy+=now.second;
    }
    cout<<fy<<"\n";
    return ;
}

signed main()
{
    int T;
    cin>>T;
    while(T--)
        Work();
    return 0; 
}