跳到正文

个人记录

模拟费用流

Basori_Tiara约 17 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月27日
篇幅
约 17 分钟

版本

  1. v1当前

一直以为这是一个很酷的东西,但是实际上并没有我想像中那样酷炫。

模拟费用流指的是,有一个弱智问题,直接上网络流/费用流就能直接解决,但是数据范围不允许,而这张图又有一些特殊性质支持你使用一些特殊的做法跑所谓的网络流/费用流的解。

例题:P6235

容易发现对角线是分类的,这个东西本质就是二分图最小权点覆盖,这个东西的解决方式是把这个转化成最小割模型。

直接跑肯定是做不了的,我们考虑一条对角线每次都会和一段区间的对角线连边,这是一个非常厉害的性质,我们相当于每次带来了 valval 的流量要流这么一段区间,那我们按照区间的右端点排序后,每次相当于是把能选的左端点的点尽量选掉,然后再拓出新的贡献来,所以这个题的流程就是,对于一个区间,从左到右枚举他所有还没满流的出边,如果这次能够让他满流就直接满流,不能的话就把流量减掉。

很帅气的操作。

例题:AT_agc018_c

一般模拟费用流的例题都是这个,但是感觉这个完全不如上面那个直观啊!

考虑建模,源点连左边所有点(1,0),右边是80plus金银铜代表点,连汇点(X,0),(Y,0),(Z,0),中间连 (1,xi),(1,yi),(1,zi).

然后你考虑这张图有什么性质呢?

他作为一张二分图,右边只有三个点,而且还保证了这三个点连出去的边全满流了。

所以我们可以枚举三个点让他们满流。

首先是第一个点,如果要满流,就只可能是直接流第一个点,用个堆就行了。

第二个点,有点意思,如果要满流,有可能是直接流第二个点,也有可能是有个空点代替第一个,然后剩下一个改流第二个。

第三个点,有点恶心。

可能是:

直流

二连三

一连三

一连二连三

一连三连二

所幸这些都能通过堆实现。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int x,y,z,n;
int A[100005];
int B[100005];
int C[100005];
struct node{
   int x,id;
   bool friend operator < (const node &a,const node &b){
      return a.x<b.x||a.x==b.x&&a.id<b.id;
   }
   bool friend operator == (const node &a,const node &b){
      return a.x==b.x&&a.id==b.id;
   }
};
struct NiChaHeap{
   priority_queue<node> ad,dl;
   void init(){
      while(!dl.empty()&&ad.top()==dl.top())ad.pop(),dl.pop();
      return;
   }
   void del(node x){
      dl.push(x);
      return;
   }
   void push(node x){
      ad.push(x);
      return;
   }
   bool empty(){
      return !(ad.size()-dl.size());
   }
   node top(){
      if(empty()){
         return node{-1000000000000000000,0};
      }
      init();
      return ad.top();
   }
}qa,qb,qc,qac,qab,qba,qbc;
void add(int id){
   qa.push(node{A[id],id});
   qb.push(node{B[id],id});
   qc.push(node{C[id],id});
}
void del(int id){
   qa.del(node{A[id],id});
   qb.del(node{B[id],id});
   qc.del(node{C[id],id});
   return;
}
void addA(int id){
   qac.push(node{C[id]-A[id],id});
   qab.push(node{B[id]-A[id],id});
   return;
}
void addB(int id){
   qbc.push(node{C[id]-B[id],id});
   qba.push(node{A[id]-B[id],id});
   return;
}
void delA(int id){
   qac.del(node{C[id]-A[id],id});
   qab.del(node{B[id]-A[id],id});
   return;
}
void delB(int id){
   qbc.del(node{C[id]-B[id],id});
   qba.del(node{A[id]-B[id],id});
   return;
}
;
int ans;
signed main(){
   scanf("%lld%lld%lld",&x,&y,&z);
   n=x+y+z;
   for(int i=1;i<=n;i++){
      scanf("%lld%lld%lld",&A[i],&B[i],&C[i]);
   }
   for(int i=1;i<=n;i++){
      add(i);
   }
   for(int i=1;i<=x;i++){
      int tmp=qa.top().x;
      int id=qa.top().id;
      addA(id);
      del(id);
      ans+=tmp;
   }
   for(int i=1;i<=y;i++){
      int ans1=qb.top().x;
      int ans2=qa.top().x+qab.top().x;
      if(ans1>ans2){
         ans+=ans1;
         addB(qb.top().id);
         del(qb.top().id);
      }
      else{
         ans+=ans2;
         int tmp=qab.top().id;
         delA(tmp);
         addB(tmp);
         tmp=qa.top().id;
         addA(tmp);
         del(tmp);
      }
   }
   for(int i=1;i<=z;i++){
      vector<node> need;
      int ans1=qc.top().x;
      int ans2=qa.top().x+qac.top().x;
      int ans3=qb.top().x+qbc.top().x;
      int ans4=qa.top().x+qab.top().x+qbc.top().x;
      int ans5=qb.top().x+qba.top().x+qac.top().x;
      need.push_back(node{ans1,1});
      need.push_back(node{ans2,2});
      need.push_back(node{ans3,3});
      need.push_back(node{ans4,4});
      need.push_back(node{ans5,5});
      sort(need.begin(),need.end());
      ans+=need[4].x;
      switch(need[4].id){
         case 1:{
            del(qc.top().id);
            break;
         }
         case 2:{
            int tmp=qac.top().id;
            delA(tmp);
            tmp=qa.top().id;
            addA(tmp);
            del(tmp);
            break;
         }
         case 3:{
            int tmp=qbc.top().id;
            delB(tmp);
            tmp=qb.top().id;
            addB(tmp);
            del(tmp);
            break;
         }
         case 4:{
            int tmp=qbc.top().id;
            delB(tmp);
            tmp=qab.top().id;
            delA(tmp);
            addB(tmp);
            tmp=qa.top().id;
            del(tmp);
            addA(tmp);
            break;
         }
         case 5:{
            int tmp=qac.top().id;
            delA(tmp);
            tmp=qba.top().id;
            delB(tmp);
            addA(tmp);
            tmp=qb.top().id;
            del(tmp);
            addB(tmp);
            break;
         }
      }
   }
   printf("%lld",ans);
   return 0;
}

例3. P5470

很有意思的题,费用流模型并没有那么明显。

先把A和B摊开,源向A_i连(1,A_i),B_i向汇连(1,B_i)

考虑至少L个重复的,也就是重复不设限制,所以A_i向B_i连边(1,0)

但是还有不重复的,这个有限制,最多K-L个,所以把这个东西作一条新边A->B,所有A_i连过来,所有B_i连过去,跑最小费用最大流就是答案。

但是没办法费用流,这个东西增广路形式单一,可以模拟费用流。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
   int x,id;
   bool friend operator < (const node &a,const node &b){
      return a.x<b.x||a.x==b.x&&a.id<b.id;
   }
   bool friend operator == (const node &a,const node &b){
      return a.x==b.x&&a.id==b.id;
   }
};
struct Heap{
   priority_queue<node> ad,dl;
   void init(){
      while(!dl.empty()&&ad.top()==dl.top())ad.pop(),dl.pop();
      return;
   }
   void del(node x){
      dl.push(x);
      return;
   }
   void push(node x){
      ad.push(x);
      return;
   }
   bool empty(){
      return !(ad.size()-dl.size());
   }
   node top(){
      if(empty()){
         return node{-1000000000000000000,0};
      }
      init();
      return ad.top();
   }
   void clear(){
      while(!ad.empty())ad.pop();
      while(!dl.empty())dl.pop();
      return;
   }
}qa,qb,qab,qanb,qbna;
int qnanb;
//a单,b单,ab连
//要么是左边有个a,他的b被占了,他没有,他直接过去给b拿下了,然后剩下的都给他塞回去了。
//实际操作的时候记录一个qanb表示没有b的a的最大值,然后跑一次相当于是加上qanb的最大值和qb的最大值。
//以此类推,qbna+qa
//然后是qnanb表示a和b都没了但是都是单,考虑这种只要出现了绝对是最高的,他的收益是qa+qb。
//所以qnanb只是计数用,如果哪次算的时候发现拿出来的id之前已经做过了,那么qnanb就带过来就行了。
int T;
int n,K,L;
int A[200005];
int B[200005];
int ans;
bool vis[400005];
void Dela(int id){
   // printf("%lld %d\n",id,vis[id]);
   qa.del(node{A[id],id});
   if(vis[n+id]){
      qanb.del(node{A[id],id});
      qnanb++;
      return;
   }
   vis[id]=true;
   qbna.push(node{B[id],id});
   qab.del(node{A[id]+B[id],id});
   return;
}
void Delb(int id){//
   qb.del(node{B[id],id});
   if(vis[id]){
      qbna.del(node{B[id],id});
      qnanb++;
      return;
   }
   vis[n+id]=true;
   qanb.push(node{A[id],id});
   qab.del(node{A[id]+B[id],id});
   return;
}
signed main(){
//   freopen("test.in","r",stdin);
//   freopen("ans.out","w",stdout);
   scanf("%lld",&T);
   while(T--){
      scanf("%lld%lld%lld",&n,&K,&L);
      for(int i=1;i<=2*n;i++){
         vis[i]=false;
      }
      qa.clear();
      qb.clear();
      qab.clear();
      qanb.clear();
      qbna.clear();
      qnanb=0,ans=0;
      for(int i=1;i<=n;i++){
         scanf("%lld",&A[i]);
      }
      for(int i=1;i<=n;i++){
         scanf("%lld",&B[i]);
      }
      for(int i=1;i<=n;i++){
         qa.push(node{A[i],i});
         qb.push(node{B[i],i});
         qab.push(node{A[i]+B[i],i});
      }
      qnanb=K-L;
      for(int i=1;i<=K;i++){
         // printf("A%lld %lld %lld %lld %lld\n",qa.top().x,qb.top().x,qab.top().x,qanb.top().x,qbna.top().x);
         // printf("%lld %lld\n",i,ans);
         if(qnanb){
            // puts("clear");
            node tmp1=qa.top();
            node tmp2=qb.top();
            ans+=tmp1.x+tmp2.x;
            // printf("%lld %lld %lld\n",tmp1.id,tmp2.id,ans);
            // if(tmp1.id==tmp2.id){//这两个相等就意味着删了一样的,不占地方直接走人。
            //   qa.del(tmp1);
            //   qb.del(tmp2);
            //   qab.del(node{tmp1.x+tmp2.x,tmp1.id});
            //   continue;
            // }
            qnanb--;
            Dela(tmp1.id);
            Delb(tmp2.id);
            continue;
         }
         //现在不能随便选了。
         int ans1=qab.top().x;
         int ans2=qanb.top().x+qb.top().x;
         int ans3=qbna.top().x+qa.top().x;
         // printf("%lld %lld %lld %lld\n",i,ans1,ans2,ans3);
         if(ans1>=ans2&&ans1>=ans3){
            ans+=ans1;
            int id=qab.top().id;
            qa.del(node{A[id],id});
            qb.del(node{B[id],id});
            qab.del(qab.top());
            //如果删两个连续的是最方便的。
         }
         else if(ans2>=ans3){//这个时候相当于是把qanb给丢掉,然后把qb给删了。
            ans+=ans2;
            qa.del(qanb.top());
            qanb.del(qanb.top());
            //因为是qanb所以不需要担心别的问题
            Delb(qb.top().id);
         }else{
            ans+=ans3;
            qb.del(qbna.top());
            qbna.del(qbna.top());
            Dela(qa.top().id);
         }
      }
      printf("%lld\n",ans);
   }
   return 0;
}
/*
1 [5] [8] 3 2 [4]
2 [6] [9] 3 1 [7]

10 9 3
1  2   [8] [3] [3]  [6] 1   [5] [2] [9]
[3] [10] [9] [4] 1   [10] [7] 2   [7]  1

10 6
3 2
6 3
8 9
5 7
4 4
9 1
118


*/
原文