百练+EK算法模版+网络流邻接表形式
来源:互联网 发布:图像边缘检测算法代码 编辑:程序博客网 时间:2024/06/05 07:31
点击打开链接
//在于建图#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=405;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;}int max_flow(int s, int t, int m){ snode=s;tnode=t; int 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,F=0,D=0; int s=0,t=0,num1=0,num2=0,num=0; scanf("%d %d %d",&N,&F,&D); memset(cap, 0, sizeof(cap)); memset(flow, 0, sizeof(flow)); memset(pre, 0, sizeof(pre)); memset(rest, 0, sizeof(rest)); s=0; t=2*N+F+D+1; for(int i=1;i<=F;i++) cap[s][i]=1; for(int i=F+2*N+1;i<=F+2*N+D;i++) cap[i][t]=1; for(int i=1;i<=N;i++) cap[F+2*i-1][F+2*i]=1; for(int i=1;i<=N;i++){ scanf("%d %d",&num1,&num2); while (num1--) { scanf("%d",&num); cap[num][F+2*i-1]=1; } while (num2--) { scanf("%d",&num); cap[F+2*i][F+2*N+num]=1; } } printf("%d\n",max_flow(s,t,t)); return 0;}
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <queue>#include <string.h>#include <vector>#define inf 0x3f3f3f3f#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int maxn=20005;struct edge{ int from,to,cap,flow; edge(int u, int v, int c, int f):from(u),to(v),cap(c),flow(f){};};vector<edge> ed;vector<int> G[maxn];int snode,tnode,pre[maxn],vis[maxn],rest[maxn];void init(int t){ for(int i=0;i<=t;i++) G[i].clear(); ed.clear();}void addEdge(int from, int to, int cap){ ed.push_back(edge(from, to, cap, 0)); ed.push_back(edge(to, from, 0, 0)); int num=ed.size(); G[from].push_back(num-2); G[to].push_back(num-1);}bool BFS_Path(int m)///augmenting path{ memset(vis, 0, sizeof(vis)); queue<int>Q; int u=0; u=snode; pre[u]=u; rest[u]=inf;vis[u]=1; Q.push(u); while (!Q.empty()) { u=Q.front(); Q.pop(); for(int i=0;i<G[u].size();i++){ edge &e=ed[G[u][i]]; if(!vis[e.to]&&e.flow<e.cap){ pre[e.to]=G[u][i];vis[e.to]=1; rest[e.to]=min(rest[u],e.cap-e.flow); if(e.to==tnode) return true; /// find augment path Q.push(e.to); } } } return false;///no augment path}int max_flow(int s, int t, int m){ snode=s;tnode=t; int ans=0; while (BFS_Path(m)) { ans+=rest[t]; //cout<<ans<<endl; /*int v=t; while (v!=s) { int u=pre[v]; ed[u].flow+=rest[t]; ed[v].flow-=rest[t]; v=u; } */ for(int e=t;e!=s;e=ed[pre[e]].from) { ed[pre[e]].flow+=rest[t]; ed[pre[e]^1].flow-=rest[t]; } } return ans;}int main(){ int N=0,M=0,A=0,B=0,a=0,b=0,w=0; memset(pre, 0, sizeof(pre)); memset(rest, 0, sizeof(rest)); scanf("%d %d",&N,&M); int s=0,t=N+1; init(t); for(int i=1;i<=N;i++){ scanf("%d %d",&A,&B); addEdge(s, i, A); addEdge(i, t, B); } for(int i=1;i<=M;i++){ scanf("%d %d %d",&a,&b,&w); ed.push_back(edge(a, b, w, 0)); ed.push_back(edge(b, a, w, 0)); int num=ed.size(); G[a].push_back(num-2); G[b].push_back(num-1); } printf("%d\n",max_flow(s,t,t)); return 0;}
阅读全文
0 0
- 百练+EK算法模版+网络流邻接表形式
- 百练+最大流+EK算法模版
- 网络流---EK模版
- 百练+网络流Dinic模版,EK空间使用不够了
- HDU 3549 最大流 EK算法 邻接表实现
- 网络流EK算法
- 网络流EK算法
- 网络流EK算法
- 网络流EK算法
- 网络流EK算法
- 网络流--EK算法
- 匈牙利算法 && EK(邻接表)
- EK算法模版
- HDU ACM 1532 Drainage Ditches->网络最大流模版题(EK算法实践)
- 网络流EK算法理解
- 网络流EK算法详解
- 网络流之EK 算法
- 最大网络流 EK 算法
- 第十二周项目(2)
- Intent传各种值
- VUE component
- 发现一个工具,可以将psd 导出成 html,搭建nodejs环境测试
- 第十一周 【项目5
- 百练+EK算法模版+网络流邻接表形式
- Shell脚本中$0, $?, $! 等含义
- Angular组件间传值
- 第11周【项目4
- 第十五周项目一——验证算法之冒泡排序
- Angular 全选
- 第十三周——项目一验证算法—(4)验证平衡二叉树相关算法
- 比KEEP更好,运动健身、塑形瘦身的小程序新选择
- AJAX校验