解题报告 P2762 太空飞行计划问题

· · 个人记录

题目链接:P2762 太空飞行计划问题。

解法:最大权闭合子图模型

最大权闭合图模型,转化为最小割去做。

证明详情参见胡伯涛论文《最小割模型在信息学竞赛中的应用》。

在本题中,所谓正权就是实验的利润,所谓负权就是仪器的费用。

如此建模,即可将问题转化为最小割模型。

关于建模

具体而言:

由于我们选择一个实验,就要选择该实验所需的所有仪器。而显然我们不会选择任何一件我们所选实验不需要的仪器。于是我们最终选出的方案一定是一些实验以及这些实验所需的仪器的集合。

由最小割的性质知,最小割 [s,t] 中的边一定不会有那些权值为 +\infty 的边,而只会包含与 s 或 t 连接的那些边。

于是若实验与 s 在同一个集合,则该实验对应的仪器也一个不落的被选入了同一个集合,故一个最小割实际上会帮我们选出一个合法的方案来。

易知一个合法的方案也一定能对应为一个最小割。

于是合法方案与最小割等价。

那么为何最小割就能选出最优的合法方案呢?

最小割相当于切掉一些实验的利润(与 s 不连通的实验),并支付所选仪器的费用(与 t 连通的仪器),当然要最小化这个割的值啦。

注意到最小割分为两部分,一部分是 s 这边某些满流的出边,另一部分是 t 那边某些满流的入边。

这两部分割有各自的意义,很巧妙。

$t$ 这边的割边对应的点,代表着一些有利可图的实验,因为把这些实验所需的费用都支付后,这些实验还有利润($s$ 出边的容量)没有被流量占据。 所以网络中的流量其实代表了费用的流向,而割由两个意义不同的部分组成。

于是最终答案就是所有实验的利润减去未选实验的利润再减去所选仪器的费用,等于所有实验的利润减去最小割的容量。

关于输出

怎么求最小割?

—— 求最大流即可。

怎么输出方案?

—— 割完之后, dfs 出所有与 s 相连通的点(即残量网络中 s 能到达的点),这些点就是最大权闭合子图的所有点。

万万不能认为 s 的满流出边指向的点就是不选的点。最大流只是求最小割的手段,这个流的形态并不能直接视作割的形态,二者很大程度上相似,但不一定相同。所以还是 dfs 靠谱。

注意事项

AC 代码

评测记录。

#include<cstdio>
#include<cstring>
#include<bitset>
#include<queue>
#include<list>
namespace Qza{
    #define MAXN 110
    #define MAXM 20810
    #define INF 2147483647
    int h[MAXN],f[MAXN],gap[MAXN];
    int en[MAXN],to[MAXM],nex[MAXM],w[MAXM];
    bool choose[MAXN];
    std::list<int> lst[MAXN];
    std::vector<std::list<int>::iterator> it;
    void add_edge(int x,int y,int v)
    {
        static int cnt=-1;
        nex[++cnt]=en[x]; to[cnt]=y; en[x]=cnt; w[cnt]=v;
        nex[++cnt]=en[y]; to[cnt]=x; en[y]=cnt; w[cnt]=0;
    }
    int bfs(int s,int t)
    {
        register int u,v,hst=0;
        std::queue<int> q;
        for(h[t]=0,q.push(t);!q.empty();) {
            u=q.front(); q.pop();
            for(int p=en[u];p!=-1;p=nex[p])
                if(w[p^1] && !h[v=to[p]] && v!=t)
                    hst=h[v]=h[u]+1,q.push(v);
        }
        return hst;
    }
    int HLPP(int s,int t,int n)
    {
        if(s==t) return INF;
        register int u,v,flow,minhv,nowh=0,hst=bfs(s,t);
        if(!h[s]) return 0;
        it.resize(n+2);
        std::vector<int> buc[MAXN<<1];
        h[s]=n;
        for(int i=1;i<=n;++i)
            if(h[i]) ++gap[h[i]], it[i]=lst[h[i]].insert(lst[h[i]].begin(),i);
            else if(i!=t) h[i]=n<<1;
        for(int p=en[s];p!=-1;p=nex[p]) 
            if(v=to[p],flow=w[p]) {
                f[s]-=flow; f[v]+=flow;
                w[p]-=flow; w[p^1]+=flow;
                if(f[v]==flow) {
                    buc[h[v]].push_back(v);
                    nowh=std::max(nowh,h[v]);
                }
            }
        while(nowh) {
            if(buc[nowh].empty()) {--nowh;continue;}
            u=buc[nowh].back();
            buc[nowh].pop_back();
            minhv=n<<1;
            for(int p=en[u];f[u] && p!=-1;p=nex[p]) 
                if(w[p]) {
                    if(h[v=to[p]]<h[u]) {
                        flow=std::min(f[u],w[p]);
                        f[u]-=flow; f[v]+=flow;
                        w[p]-=flow; w[p^1]+=flow;
                        if(f[v]==flow) buc[h[v]].push_back(v);
                    }
                    else minhv=std::min(minhv,h[v]);
                }
            if(!f[u]) continue;
            if(h[u]<n && !(--gap[h[u]])) {
                for(int i=h[u]+1;i<=hst;++i) {
                    for(std::list<int>::iterator p=lst[i].begin();p!=lst[i].end();++p)
                        h[*p]=n+1;
                    gap[i]=0;
                    lst[i].clear();
                }
                hst=h[u]-1;
                lst[h[u]].clear();
                nowh=h[u]=n+1;
                buc[nowh].push_back(u);
            }
            else {
                if(h[u]<n) it[u]=lst[h[u]].erase(it[u]);
                nowh=h[u]=minhv+1;
                if(h[u]<n) {
                    it[u]=lst[nowh].insert(lst[nowh].begin(),u);
                    ++gap[nowh];
                    hst=std::max(hst,h[u]);
                }
                buc[nowh].push_back(u);
            }
        }
        return f[t];
    }
    void dfs(int x,int fa)
    {
        choose[x]=true;
        for(int p=en[x];p!=-1;p=nex[p])
            if(w[p] && to[p]!=fa && !choose[to[p]]) dfs(to[p],x);
    }
    int read()
    {
        char ch=getchar();int x=0;
        while(ch<'0' || ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=getchar();
        return x;
    }
    void main()
    {
        int n,m,s,t,x,val_posi=0,maxflow;
        char ch;
        memset(en,0xff,sizeof(en));
        m=read(); n=read();
        s=n+m+1; t=n+m+2;
        for(int i=1;i<=m;++i) {
            add_edge(s,i,read());
            ch=0;
            while(ch!='\n') {
                x=0;
                while((ch<'0' || ch>'9')&&ch!='\n') ch=getchar();
                while (ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=getchar();
                if(x) add_edge(i,x+m,INF);
            }
        }
        for(int i=1;i<=n;++i) {
            add_edge(i+m,t,read());
        }
        for(int p=en[s];p!=-1;p=nex[p])
            if(w[p]) val_posi+=w[p];
        maxflow=HLPP(s,t,n+m+2);
        dfs(s,0);   // 搜索所有与 s 相连的点,即为最大权闭合子图中的点。 
        for(int i=1;i<=m;++i) if(choose[i  ]) printf("%d ",i);putchar('\n');
        for(int i=1;i<=n;++i) if(choose[i+m]) printf("%d ",i);putchar('\n');
        printf("%d",val_posi-maxflow);
        return ;
    }
}
int main()
{
    Qza::main();
    return 0;
}