最大流Dinic(模板)
时间:2019-11-03
本文章向大家介绍最大流Dinic(模板),主要包括最大流Dinic(模板)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 #include <cassert> 21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 24 //****************** 25 int abss(int a); 26 int lowbit(int n); 27 int Del_bit_1(int n); 28 int maxx(int a,int b); 29 int minn(int a,int b); 30 double fabss(double a); 31 void swapp(int &a,int &b); 32 clock_t __STRAT,__END; 33 double __TOTALTIME; 34 void _MS(){__STRAT=clock();} 35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 36 //*********************** 37 #define rint register int 38 #define fo(a,b,c) for(rint a=b;a<=c;++a) 39 #define fr(a,b,c) for(rint a=b;a>=c;--a) 40 #define mem(a,b) memset(a,b,sizeof(a)) 41 #define pr printf 42 #define sc scanf 43 #define ls rt<<1 44 #define rs rt<<1|1 45 typedef vector<int> VI; 46 typedef long long ll; 47 const double E=2.718281828; 48 const double PI=acos(-1.0); 49 //const ll INF=(1LL<<60); 50 const int inf=(1<<30); 51 const double ESP=1e-9; 52 const int mod=(int)1e9+7; 53 const int N=5500; 54 const int M=5e5+10; 55 56 class DINIC 57 { 58 public: 59 // const int MAXN=10004,MAXWAY=100005; 60 int n,way,max_flow,deep[N]; 61 int tot,head[N],cur[N]; 62 struct EDGE{ 63 int to,next; 64 int dis; 65 }edge[M]; 66 void Init(int n_) 67 { 68 tot=-1;//因为加反向边要^1,所以要从0开始; 69 n=n_; 70 max_flow=0; 71 for(int i=0;i<=n_;++i) 72 head[i]=-1; 73 } 74 void add(int from,int to,int V) 75 { 76 //正向 77 ++tot; 78 edge[tot].to=to; 79 edge[tot].dis=V; 80 edge[tot].next=head[from]; 81 head[from]=tot; 82 //反向 83 swap(from,to); 84 ++tot; 85 edge[tot].to=to; 86 edge[tot].dis=0; 87 edge[tot].next=head[from]; 88 head[from]=tot; 89 } 90 queue<int>q; 91 bool bfs(int s,int t) 92 { 93 for(int i=1;i<=n;++i) 94 deep[i]=inf; 95 while(!q.empty())q.pop(); 96 for(int i=1;i<=n;++i)cur[i]=head[i]; 97 deep[s]=0; 98 q.push(s); 99 100 while(!q.empty()) 101 { 102 int now=q.front();q.pop(); 103 for(int i=head[now];i!=-1;i=edge[i].next) 104 { 105 if(deep[edge[i].to]==inf&&edge[i].dis) 106 { 107 deep[edge[i].to]=deep[now]+1; 108 q.push(edge[i].to); 109 } 110 } 111 } 112 return deep[t]<inf; 113 } 114 int dfs(int now,int t,int limit) 115 { 116 if(!limit||now==t)return limit; 117 int flow=0,f; 118 for(int i=cur[now];i!=-1;i=edge[i].next) 119 { 120 cur[now]=i; 121 if(deep[edge[i].to]==deep[now]+1&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis)))) 122 { 123 flow+=f; 124 limit-=f; 125 edge[i].dis-=f; 126 edge[i^1].dis+=f; 127 if(!limit)break; 128 } 129 } 130 return flow; 131 } 132 void Dinic(int s,int t) 133 { 134 while(bfs(s,t)) 135 max_flow+=dfs(s,t,inf); 136 } 137 }G; 138 int a[N][N],color[N][N]; 139 140 int main() 141 { 142 int n,m; 143 while(~sc("%d%d",&n,&m)) 144 { 145 ll TOT=0; 146 for(int i=1;i<=n;++i) 147 for(int j=1;j<=m;++j) 148 sc("%d",&a[i][j]),TOT+=a[i][j]; 149 G.Init(n*m+2); 150 int S=n*m+1,T=n*m+2,cnt=0; 151 for(int i=1;i<=n;++i) 152 { 153 for(int j=1;j<=m;++j) 154 { 155 if(i&1) 156 { 157 if(j&1) 158 color[i][j]=1; 159 else 160 color[i][j]=0; 161 } 162 else 163 { 164 if(j&1) 165 color[i][j]=0; 166 else 167 color[i][j]=1; 168 } 169 } 170 } 171 for(int i=1;i<=n;++i) 172 { 173 for(int j=1;j<=m;++j) 174 { 175 ++cnt; 176 if(color[i][j]==0) 177 { 178 G.add(S,cnt,a[i][j]); 179 if(i>1)G.add(cnt,cnt-m,inf); 180 if(j>1)G.add(cnt,cnt-1,inf); 181 if(i<n)G.add(cnt,cnt+m,inf); 182 if(j<m)G.add(cnt,cnt+1,inf); 183 } 184 else 185 G.add(cnt,T,a[i][j]); 186 } 187 } 188 /* fo(i,1,n) 189 { 190 fo(j,1,m) 191 pr("%d ",color[i][j]); 192 pr("\n"); 193 }*/ 194 G.Dinic(S,T); 195 pr("%lld\n",TOT-G.max_flow); 196 } 197 return 0; 198 } 199 200 /**************************************************************************************/ 201 202 int maxx(int a,int b) 203 { 204 return a>b?a:b; 205 } 206 207 void swapp(int &a,int &b) 208 { 209 a^=b^=a^=b; 210 } 211 212 int lowbit(int n) 213 { 214 return n&(-n); 215 } 216 217 int Del_bit_1(int n) 218 { 219 return n&(n-1); 220 } 221 222 int abss(int a) 223 { 224 return a>0?a:-a; 225 } 226 227 double fabss(double a) 228 { 229 return a>0?a:-a; 230 } 231 232 int minn(int a,int b) 233 { 234 return a<b?a:b; 235 }
原文地址:https://www.cnblogs.com/--HPY-7m/p/11785832.html
- 育碧与研究所共同开发的AI,是什么样的?
- 大家都在关注AI,但这些事你可能并不知道!
- AI—未来医疗
- 微信小程序新功能上线 一键连Wi-Fi手机变门禁卡
- 使用xUnit为.net core程序进行单元测试(中)
- Asp.Net 用Jquery和一般处理程序实现无刷新上传大文件
- WCF技术剖析之十五:数据契约代理(DataContractSurrogate)在序列化中的作用
- WCF技术剖析之十四:泛型数据契约和集合数据契约(上篇)
- WCF技术剖析之十四:泛型数据契约和集合数据契约(下篇)
- WCF技术剖析(卷1)之前言
- WCF技术剖析(卷1)之目录
- WCF技术剖析(卷1)之推荐序
- 谈谈基于SQL Server 的Exception Handling[上篇]
- 谈谈WCF中的Data Contract(4):WCF Data Contract Versioning
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 没想到竟是因为它!让我的服务器变成了别人的挖矿工具
- 从零到一,Serverless 平台在滴滴内部落地
- React 使用 Proxy 代理(create-react-app)
- .Net Core + EF + mysql 从数据库生成实体
- Git 常用命令
- Nodejs 一些细节 (持续更新)
- Jenkins 凭据使用
- React源码解读【一】API复习与基础
- choco 安装 和 mkcert 本地https
- js 函数柯里化(Currying)
- GPS数据Python解析及地图可视化
- 文稿:Ant Design从无到有,带你体悟大厂前端开发范式
- 在React中实现和Vue一样舒适的keep-alive
- uniapp获取接口数据,渲染在picker选择器里面
- 我们是这样一步一步实现分布式锁的