[关闭]
@exut 2024-11-11T23:34:33.000000Z 字数 3928 阅读 136

网络流建模练习

刷题


不定时更新

P1402 酒店之王

先将每个人建一个点,再建一个总源点向所有房间连边流量为1,每间房向对应的客人连边也是1,客人向每个喜欢的菜连1边,菜全部向汇点连1边

这样做存在问题,没有约束一个人只能拿一个,考虑拆点约束,把一个客人拆成两个,一个负责接受房间,一个负责拿菜,两个点之间连1边

就好了

代码实现是ez的

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=3005;
  4. const int inf=1e9;
  5. struct edge{
  6. int v,w,nxt;
  7. }e[N*2];
  8. int hd[N],cur[N],tot=1;
  9. void add(int u,int v,int w){
  10. e[++tot]={v,w,hd[u]},hd[u]=tot;
  11. e[++tot]={u,0,hd[v]},hd[v]=tot;
  12. }
  13. int n,p,q;
  14. int s,t;
  15. int dis[N];
  16. bool bfs(){
  17. for(int i=1;i<=t;i++) dis[i]=inf;
  18. dis[s]=0;
  19. queue<int> Q;
  20. Q.push(s);
  21. cur[s]=hd[s];
  22. while(!Q.empty()){
  23. int u=Q.front();
  24. Q.pop();
  25. for(int i=hd[u];i;i=e[i].nxt){
  26. int v=e[i].v;
  27. if(e[i].w>0 and dis[v]==inf){
  28. cur[v]=hd[v];
  29. dis[v]=dis[u]+1;
  30. Q.push(v);
  31. if(v==t) return 1;
  32. }
  33. }
  34. }
  35. return 0;
  36. }
  37. int dinic(int u,int fl){
  38. if(u==t) return fl;
  39. int res=0;
  40. for(int i=cur[u];i;i=e[i].nxt){
  41. int v=e[i].v;
  42. cur[u]=i;
  43. if(e[i].w>0 and dis[v]==dis[u]+1){
  44. int k=dinic(v,min(fl,e[i].w));
  45. if(!k) dis[v]=inf;
  46. fl-=k,res+=k;
  47. e[i].w-=k,e[i^1].w+=k;
  48. }
  49. if(!fl) break;
  50. }
  51. return res;
  52. }
  53. int main(){
  54. cin>>n>>p>>q;
  55. s=2*n+p+q+1,t=s+1;
  56. for(int i=1;i<=n;i++){
  57. for(int j=1;j<=p;j++){
  58. int tmp;cin>>tmp;
  59. if(tmp) add(2*n+j,i,1);
  60. }
  61. }
  62. for(int i=1;i<=n;i++){
  63. for(int j=1;j<=q;j++){
  64. int tmp;cin>>tmp;
  65. if(tmp) add(i+n,2*n+p+j,1);
  66. }
  67. }
  68. for(int i=1;i<=n;i++) add(i,i+n,1);
  69. for(int i=1;i<=p;i++){
  70. add(s,2*n+i,1);
  71. }
  72. for(int i=1;i<=q;i++){
  73. add(2*n+p+i,t,1);
  74. }
  75. int ans=0;
  76. while(bfs()){
  77. ans+=dinic(s,inf);
  78. }
  79. cout<<ans;
  80. }

励志把dinic写的和LCT一样熟练(?)

P2756 飞行员配对方案问题

一眼丁真,鉴定为二分图匹配

参考上一题,源点连1向左部点,正常输入的边由左连右,也是1,最后右向汇连1

输出方案考虑 遍历左部点,看哪条残量网络为0就配对

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=2e4+5;
  4. const int inf=1e9;
  5. int n,m;
  6. int s,t;
  7. struct edge{
  8. int v,w,nxt;
  9. }e[N*2];
  10. int hd[N],cur[N],tot=1;
  11. void add(int u,int v,int w){
  12. e[++tot]={v,w,hd[u]},hd[u]=tot;
  13. e[++tot]={u,0,hd[v]},hd[v]=tot;
  14. }
  15. int dis[N];
  16. bool bfs(){
  17. for(int i=1;i<=t;i++) dis[i]=inf;
  18. dis[s]=0;
  19. queue<int> Q;
  20. Q.push(s);
  21. cur[s]=hd[s];
  22. while(!Q.empty()){
  23. int u=Q.front();
  24. Q.pop();
  25. for(int i=hd[u];i;i=e[i].nxt){
  26. int v=e[i].v;
  27. if(dis[v]==inf and e[i].w>0){
  28. dis[v]=dis[u]+1;
  29. Q.push(v);
  30. cur[v]=hd[v];
  31. if(v==t) return 1;
  32. }
  33. }
  34. }
  35. return 0;
  36. }
  37. int dinic(int u,int fl){
  38. if(u==t) return fl;
  39. int res=0;
  40. for(int i=cur[u];i;i=e[i].nxt){
  41. cur[u]=i;
  42. int v=e[i].v;
  43. if(e[i].w and dis[v]==dis[u]+1){
  44. int k=dinic(v,min(fl,e[i].w));
  45. if(!k) dis[v]=inf;
  46. res+=k,fl-=k;
  47. e[i].w-=k,e[i^1].w+=k;
  48. }
  49. if(!fl) break;
  50. }
  51. return res;
  52. }
  53. int main(){
  54. ios::sync_with_stdio(0);
  55. cin.tie(0),cout.tie(0);
  56. cin>>n>>m;
  57. s=m+1,t=s+1;
  58. for(int i=1;i<=n;i++){
  59. add(s,i,1);
  60. }
  61. for(int i=n+1;i<=m;i++){
  62. add(i,t,1);
  63. }
  64. while(1){
  65. int u,v;
  66. cin>>u>>v;
  67. if(u==-1) break;
  68. add(u,v,1);
  69. }
  70. int ans=0;
  71. while(bfs()){
  72. ans+=dinic(s,inf);
  73. }
  74. cout<<ans<<"\n";
  75. for(int u=1;u<=n;u++){
  76. for(int i=hd[u];i;i=e[i].nxt){
  77. if(e[i].v!=s and e[i].w==0){
  78. cout<<u<<" "<<e[i].v<<"\n";
  79. break;
  80. }
  81. }
  82. }
  83. }

P1345 [USACO5.4] 奶牛的电信

原图正常边连inf流量,一个点拆成两个一个处理进边一个处理出边连1流量,然后转化为了最小割模型,跑最大流即可

顺带一提的是拆点的映射方式,除了可以是 以外也可以类似线段树写

当然习惯还是写 版本了(

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=5e4+5;
  4. const int inf=1e9;
  5. struct edge{
  6. int v,w,nxt;
  7. }e[N*2];
  8. int hd[N],cur[N],tot=1;
  9. void add(int u,int v,int w){
  10. e[++tot]={v,w,hd[u]},hd[u]=tot;
  11. e[++tot]={u,0,hd[v]},hd[v]=tot;
  12. }
  13. int n,m,s,t;
  14. int dis[N];
  15. bool bfs(){
  16. for(int i=1;i<=n*2;i++){
  17. dis[i]=inf;
  18. }
  19. dis[s]=1;
  20. queue<int> Q;
  21. Q.push(s);
  22. cur[s]=hd[s];
  23. while(!Q.empty()){
  24. int u=Q.front();
  25. Q.pop();
  26. for(int i=hd[u];i;i=e[i].nxt){
  27. int v=e[i].v;
  28. if(e[i].w>0 and dis[v]==inf){
  29. dis[v]=dis[u]+1;
  30. cur[v]=hd[v];
  31. Q.push(v);
  32. if(v==t) return 1;
  33. }
  34. }
  35. }
  36. return 0;
  37. }
  38. int dinic(int u,int fl){
  39. if(u==t) return fl;
  40. int res=0;
  41. for(int i=cur[u];i;i=e[i].nxt){
  42. int v=e[i].v;
  43. cur[u]=i;
  44. if(e[i].w>0 and dis[v]==dis[u]+1){
  45. int k=dinic(v,min(fl,e[i].w));
  46. if(!k) dis[v]=inf;
  47. res+=k,fl-=k;
  48. e[i].w-=k,e[i^1].w+=k;
  49. }
  50. if(!fl) break;
  51. }
  52. return res;
  53. }
  54. int main(){
  55. ios::sync_with_stdio(0);
  56. cin.tie(0),cout.tie(0);
  57. cin>>n>>m>>s>>t;
  58. s+=n;
  59. for(int i=1;i<=n;i++){
  60. add(i,i+n,1);
  61. }
  62. for(int i=1;i<=m;i++){
  63. int u,v;
  64. cin>>u>>v;
  65. add(n+u,v,inf);
  66. add(v+n,u,inf);
  67. }
  68. int ans=0;
  69. while(bfs()){
  70. ans+=dinic(s,inf);
  71. }
  72. cout<<ans;
  73. }

最小割边数量

在最小割的前提下求最小割边数量,只需要先跑一次然后把没有满流的边设为inf,满流设为1,再跑一遍即可

两者取一模型

将一个集合的元素(设集合为 )划分入 两个集合,对于元素 ,其分入两个集合分别可以获得 的价值,除此之外还可以有若干组合使得某些物品同时放入某个集合有额外的价值,求最大价值

我们先不考虑组合的存在,我们可以建模: ,答案就是边权总和减去最小割,显然的

这里涉及到一个建模技巧:在最小割建模中,如果若干条边要断一条后就会导致不连通,但是只有一条是符合题目意思真的想断的,那就把另外几个全部赋inf就好了。无穷大是不会纳入最小割的,一定不优

于是我们让 向组合 连价值边,然后组合 向目标点连inf边

全放 的同理

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注