ISAP 模板
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <queue>
- using namespace std;
- const int INF=;
- const int maxn=,maxm=;
- int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn];
- void addedge(int a,int b,int c)
- {
- nxt[++cnt]=fir[a];
- to[cnt]=b;
- cap[cnt]=c;
- fir[a]=cnt;
- }
- bool BFS(int S,int T)
- {
- memset(dis,,sizeof(dis));
- dis[T]=;
- queue<int>q;q.push(T);
- while(!q.empty())
- {
- int node=q.front();q.pop();
- for(int i=fir[node];i;i=nxt[i])
- {
- if(dis[to[i]])continue;
- dis[to[i]]=dis[node]+;
- q.push(to[i]);
- }
- }
- return dis[S];
- }
- int fron[maxn];
- int ISAP(int S,int T)
- {
- if(!BFS(S,T))
- return ;
- for(int i=;i<=T;i++)++gap[dis[i]];
- int p=S,ret=;
- memcpy(fron,fir,sizeof(fir));
- while(dis[S]<=T)
- {
- if(p==T){
- int f=INF;
- while(p!=S){
- f=min(f,cap[path[p]]);
- p=to[path[p]^];
- }
- p=T;ret+=f;
- while(p!=S){
- cap[path[p]]-=f;
- cap[path[p]^]+=f;
- p=to[path[p]^];
- }
- }
- int &ii=fron[p];
- for(;ii;ii=nxt[ii]){
- if(!cap[ii]||dis[to[ii]]+!=dis[p])
- continue;
- else
- break;
- }
- if(ii){
- p=to[ii];
- path[p]=ii;
- }
- else{
- if(--gap[dis[p]]==)break;
- int minn=T+;
- for(int i=fir[p];i;i=nxt[i])
- if(cap[i])
- minn=min(minn,dis[to[i]]);
- gap[dis[p]=minn+]++;
- fron[p]=fir[p];
- if(p!=S)
- p=to[path[p]^];
- }
- }
- return ret;
- }
- void Init()
- {
- memset(fir,,sizeof(fir));
- memset(gap,,sizeof(gap));
- cnt=;
- }
- int main()
- {
- int n,m;
- while(~scanf("%d%d",&m,&n))
- {
- Init();
- for(int i=;i<=m;i++){
- int x,y,c;
- scanf("%d%d%d",&x,&y,&c);
- addedge(x,y,c);
- addedge(y,x,);
- }
- printf("%d\n",ISAP(,n));
- }
- return ;
- }
后来又打了一遍,代码风格都变了。(这个没有多组数据)
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <queue>
- using namespace std;
- const int INF=;
- const int maxn=;
- const int maxm=;
- int cnt=,fir[maxn],to[maxm],nxt[maxm],cap[maxm];
- void addedge(int a,int b,int c){
- nxt[++cnt]=fir[a];
- fir[a]=cnt;
- cap[cnt]=c;
- to[cnt]=b;
- }
- queue<int>q;
- int dis[maxn];
- bool BFS(int s,int t){
- dis[t]=;q.push(t);
- while(!q.empty()){
- int x=q.front();q.pop();
- for(int i=fir[x];i;i=nxt[i])
- if(!dis[to[i]]){
- dis[to[i]]=dis[x]+;
- q.push(to[i]);
- }
- }
- return dis[s];
- }
- int fron[maxn];
- int gap[maxn],path[maxn];
- int ISAP(int s,int t){
- if(!BFS(s,t))return ;
- for(int i=s;i<=t;i++)++gap[dis[i]];
- for(int i=s;i<=t;i++)fron[i]=fir[i];
- int p=s,ret=,f;
- while(dis[s]<=t){
- if(p==t){
- f=INF;
- while(p!=s){
- f=min(f,cap[path[p]]);
- p=to[path[p]^];
- }
- ret+=f;p=t;
- while(p!=s){
- cap[path[p]]-=f;
- cap[path[p]^]+=f;
- p=to[path[p]^];
- }
- }
- int &ii=fron[p];
- for(;ii;ii=nxt[ii])
- if(cap[ii]&&dis[p]==dis[to[ii]]+)
- break;
- if(ii)
- path[p=to[ii]]=ii;
- else{
- if(--gap[dis[p]]==)break;
- int minn=t+;
- for(int i=fir[p];i;i=nxt[i])
- if(cap[i])minn=min(minn,dis[to[i]]);
- ++gap[dis[p]=minn+];ii=fir[p];
- if(p!=s)p=to[path[p]^];
- }
- }
- return ret;
- }
- int main(){
- int n,m;
- scanf("%d%d",&m,&n);
- for(int i=,a,b,c;i<=m;i++){
- scanf("%d%d%d",&a,&b,&c);
- addedge(a,b,c);
- addedge(b,a,);
- }
- printf("%d\n",ISAP(,n));
- return ;
- }
又打一遍,很好看的结构体。
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <queue>
- using namespace std;
- const int maxn=;
- const int maxm=;
- const int INF=;
- int cnt,tot,fir[maxn],fron[maxn],dis[maxn];
- int to[maxm],nxt[maxm],gap[maxn],path[maxn];
- int cap[maxm];queue<int>q;
- struct Max_Flow{
- void Init(int tot_=){
- tot=tot_;cnt=;
- memset(fir,,sizeof(fir));
- memset(dis,,sizeof(dis));
- memset(gap,,sizeof(gap));
- }
- void add(int a,int b,int c){
- nxt[++cnt]=fir[a];
- fir[a]=cnt;
- cap[cnt]=c;
- to[cnt]=b;
- }
- void addedge(int a,int b,int c){
- add(a,b,c);
- add(b,a,);
- }
- bool BFS(int s,int t){
- dis[t]=;q.push(t);
- while(!q.empty()){
- int x=q.front();q.pop();
- for(int i=fir[x];i;i=nxt[i])
- if(!dis[to[i]]){
- dis[to[i]]=dis[x]+;
- q.push(to[i]);
- }
- }
- return dis[s];
- }
- int Aug(int s,int t,int &p){
- int f=INF;
- while(p!=s){
- f=min(f,cap[path[p]]);
- p=to[path[p]^];
- }p=t;
- while(p!=s){
- cap[path[p]]-=f;
- cap[path[p]^]+=f;
- p=to[path[p]^];
- }
- return f;
- }
- int ISAP(int s,int t){
- if(!BFS(s,t))return ;
- for(int i=s;i<=t;i++)fron[i]=fir[i];
- for(int i=s;i<=t;i++)gap[dis[i]]+=;
- int p=s,ret=;
- while(dis[s]<=tot){
- if(p==t)ret+=Aug(s,t,p);
- for(int &i=fron[p];i;i=nxt[i])
- if(cap[i]&&dis[p]==dis[to[i]]+){
- path[p=to[i]]=i;
- break;
- }
- if(!fron[p]){
- if(--gap[dis[p]]==)
- break;
- int Min=tot;
- for(int i=fir[p];i;i=nxt[i])
- if(cap[i])Min=min(Min,dis[to[i]]);
- gap[dis[p]=Min+]+=;fron[p]=fir[p];
- if(p!=s)p=to[path[p]^];
- }
- }
- return ret;
- }
- }isap;
ISAP 模板的更多相关文章
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- 【CodeVS 1993】草地排水 isap模板题
开始网络流的学习,更新一下isap的模板 #include<cstdio> #include<cstring> #include<algorithm> #defin ...
- 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题
[题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...
- 最大流算法 ISAP 模板 和 Dinic模板
ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...
- HDU 3572 Task Schedule(ISAP模板&&最大流问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si , ...
- Dinic 与 SAP(ISAP?) 模板
发一个最大流模板 DinicDinicDinic //vis为int类型 //sz为总点数 namespace Dinic { inline bool bfs() { int head = 0, ta ...
- 最大流isap模板
isap+bfs初始化+栈优化,点的编号从0开始: ; ; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow; }ed ...
- ISAP模板
#include<bits/stdc++.h> using namespace std; using namespace std; typedef long long ll; const ...
- LOJ 101 最大流(ISAP 模板)
开long long的最大流 #include<bits/stdc++.h> using namespace std; ;//点数的最大值 ;//边数的最大值 ; struct Edge ...
随机推荐
- Linux系统下查看USB设备名及使用USB设备
1.系统插入USB设备后,从控制台界面有如下提示: 从控制台信息可以看出插入的USB设备名. 从上图可以看出,插入的USB设备为sde4. 但是,如果是CRT工具远程连接过去,可以使用下面的命令来查看 ...
- rabbitmq pika connection closed
You are here: Home / rabbitmq pika connection closed rabbitmq pika connection closed By lijiejie on ...
- JavaScript 使用
HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head> 部分 ...
- 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。
错误: 解决方案: 1."设置应用程序池默认属性"/"常规"/"启用32位应用程序",设置为 true. 如下图所示:(已测试,好使) 方法 ...
- 利用SQLiteOpenHelper创建数据库,进行增删改查操作
Android中提供SQLiteOpenHelper类,在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承和扩展SQLiteOpenHelper类主要做的工作就是重写 ...
- php函数serialize()与unserialize()
serialize()和unserialize()在php手册上的解释是: serialize — Generates a storable representation of a value ser ...
- PL/SQL Developer远程连接Oracle数据库
首先打开电脑,到pl/sql安装的指定目录[D:\app\DZL\product\11.2.0\dbhome_1\NETWORK\ADMIN]找到[tnsnames.ora] 打开[tnsna ...
- Ojbect-C2 3、NSArray和NSMutableArray数组的使用
Adopted Protocols NSCoding encodeWithCoder: initWithCoder: NSCopying copyWithZone: NSMutableCopying ...
- JavaScript - 测试 jQuery
测试 JavaScript 框架库 - jQuery 引用 jQuery 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 <script> 标签,其 s ...
- wpf 自定义RadioButton控件样式
实现的效果为: 我感觉来自定义RadioButton样式和定义button空间的样式差不多,只是类型不同而已. 接下来分析一下样式代码: <!--自定义单选按钮样式--> & ...