最大流+EK算法在于建图
来源:互联网 发布:福州天勤网络 编辑:程序博客网 时间:2024/06/03 19:38
点击打开链接
//建立一个超级源点和一个超级汇点,将cows与超级源点相连,容量为1//stalls与汇点连接,容量为1,然后cows与stalls连接,容量为1//求出最大流即可。// main.cpp// test// Created by 吴有堃 on 2017/9/11// Copyright © 2017年 吴有堃. All rights reserved#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string>#include <cstring>#include <string.h>#include <math.h>#include <algorithm>#include <queue>#include <vector>#include <map>#define LL long long#define inf 0x3f3f3f3f#define mod 1e9+7using namespace std;const int maxn=505;LL cap[maxn][maxn],flow[maxn][maxn],rest[maxn];int snode,tnode,pre[maxn],vis[maxn];bool BFS_Path(int m)//augmenting path{ memset(vis, 0, sizeof(vis)); queue<int>Q; int u=0,v=0; u=snode; pre[u]=u; rest[u]=inf;vis[u]=1; Q.push(u); while (!Q.empty()) { u=Q.front(); Q.pop(); for(v=0;v<=m;v++){ if(!vis[v]&&flow[u][v]<cap[u][v]){ pre[v]=u;vis[v]=1; rest[v]=min(rest[u],cap[u][v]-flow[u][v]); if(v==tnode) return true; // find augment path Q.push(v); } } } return false;//no augment path}LL max_flow(int s, int t, int m){ snode=s;tnode=t; LL ans=0; while (BFS_Path(m)) { ans+=rest[t]; int v=t; while (v!=s) { int u=pre[v]; flow[u][v]+=rest[t]; flow[v][u]-=rest[t]; v=u; } } return ans;}int main(){ int N=0,M=0; while (scanf("%d %d",&N,&M)!=EOF) { memset(cap, 0, sizeof(cap)); memset(flow, 0, sizeof(flow)); memset(pre, 0, sizeof(pre)); memset(rest, 0, sizeof(rest)); int s=0,t=N+M+1,num=0,a=0; for(int i=1;i<=N;i++){ scanf("%d",&num); cap[s][i]=1; //源点s与每头牛链接; while (num--) { scanf("%d",&a); cap[i][N+a]=1;//每头牛和对应的stall链接 } } for(int i=1;i<=M;i++){//每个stall与汇点t相联 cap[i+N][t]=1; } cout<<max_flow(s,t,t)<<endl; } return 0;}
阅读全文
0 0
- 最大流+EK算法在于建图
- 最大流EK算法
- 最大流EK算法
- 最大流-EK算法
- 最大流-EK算法
- 最大流EK算法模板
- 最大流 EK算法详解
- hdu1532 最大流 EK算法
- 图论 最大流EK算法
- 最大网络流 EK 算法
- 最大流之EK算法
- 最大流EK算法模板
- 网络最大流 EK算法
- 最大流 EK算法 (转)
- 图论算法-网络最大流【EK;Dinic】
- EK算法(网络流,最大流)
- 网络流--求最大流:EK算法
- POJ3281 Dining,最大流EK算法
- 多条目 加载
- Android中实现静态的默认安装和卸载应用
- 第四十二天 Linux系统03
- vue expose-loader 引入jQuery Error in created hook: "ReferenceError: $ is not defined"
- storm开发手册
- 最大流+EK算法在于建图
- linux下python基础调试
- JavaScript Array对象
- 问题 couldn't find "libijkffmpeg.so" 解决
- android6.0编译失败
- 智能TV开发笔记(三)简单的焦点控制--继续篇
- c++连接sqlserver的两种方式
- Android.Intent.Action
- MemberShip好文章