網絡流之最大流問題

Reference:

http://blog.csdn.net/rrerre/article/details/6751520

http://blog.csdn.net/y990041769/article/details/21026445

http://www.nocow.cn/index.php/Translate:USACO/NetworkFlow

?

最大流Edmonds_Karp算法模板:

EK算法即增廣路算法。

最大流最小割定理:最大流等于最小割

見白書P210

?

算法思想:

step 1. 令所有弧的流量為0,從而構造一個流量為0的可行流f(稱作零流)。
step 2. 若f中找不到可改進路則轉step 5;否則找到任意一條可改進路P。
step 3. 根據P求delta。
step 4. 以delta為改進量,更新可行流f。轉step 2。
step 5. 算法結束。此時的f即為最大流。

算法的關鍵步驟是step 2,即:判斷是否存在可改進路,若存在又如何求。
可以考慮用廣度優先搜索。設置標志數組,記錄頂點是不是被訪問過;使用隊列來存儲已經訪問過的頂點;另用一個一維數組p[i],記錄每個頂點是由哪個頂點擴展而來(即記錄父親節點)。
首先S入隊列。然后每次取隊首頂點v,分析所有與v相鄰的未訪問頂點u:
1、存在弧<v, u>(正向弧),且u未訪問。若f(v,u)<C(v,u)(非飽和弧),那么u入隊列,給u打上“已訪問”的標志,記u的父親節點為v。
2、存在弧<u, v>(反向弧),且u未訪問。若f(u,v) > 0(非零流弧),那么u入隊列,給u打上“已訪問”的標志,記u的父親節點為-v。(以示和正向弧的區別)。
擴展完成后,若T還沒有被訪問就必然不存在可改進路;否則就從T出發,根據記錄好的每個頂點的父親節點信息,順藤摸瓜,找出可改進路(同時還可以計算出delta)。

?

起點st,終點m

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
int n,m,st,en;
int cap[300][300];    //cap[u][v]:邊(u,v)上的最大流量
int flow[300][300];    //flow[u][v]:邊(u,v)上當前的流量
int a[300];        //a[u]:訪問標記,同時還記錄下delta值
int p[300];        //記錄父節點用
const int inf=100000000;int EK()            //st-->m
{queue<int> Q;memset(flow,0,sizeof(flow));memset(p,-1,sizeof(p));int f=0,minflow=inf;while(1){memset(a,0,sizeof(a));a[st]=inf;          Q.push(st);         while(!Q.empty()){int u=Q.front();Q.pop();for(int v=1;v<=m;v++)if(!a[v]&&cap[u][v]>flow[u][v]){p[v]=u;Q.push(v);a[v]=a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v];}}if(a[m]==0) break;for (int u=m;u!=st;u=p[u])          {flow[p[u]][u]+=a[m];flow[u][p[u]]-=a[m];}f+=a[m];}return f;
}int main()
{int S,E,C;while (cin>>n>>st>>m)       //st->m
    {memset(cap,0,sizeof(cap));for (int i=1;i<=n;i++){cin>>S>>E>>C;cap[S][E]+=C;    //處理重邊。有些題目,一條路上先給了容量30,然后重復了一次50,這時候這條路上的容量應該是30+50。
        }cout<<EK()<<endl;}return 0;
}
View Code

?

模板例題:

hdu1532

?

補充:需要拆點的問題:POJ3281

http://www.2cto.com/kf/201210/164289.html

http://moxi466839201.blog.163.com/blog/static/18003841620112316351118/

?

-------------------------------------------------------------------------------------------

補充個ISAP模板,比EK算法快,但是難想難寫。看不懂T^T...

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;
typedef  struct {int v,next,val;} edge;
const int MAXN=20010;
const int MAXM=500010;
edge e[MAXM];
int p[MAXN],eid;
void init(){memset(p,-1,sizeof(p));eid=0;}
void insert1(int from,int to,int val) //有向
{e[eid].v=to;e[eid].val=val;e[eid].next=p[from];p[from]=eid++;swap(from,to);e[eid].v=to;e[eid].val=0;e[eid].next=p[from];p[from]=eid++;
}
void insert2(int from,int to,int val) //無向
{e[eid].v=to;e[eid].val=val;e[eid].next=p[from];p[from]=eid++;swap(from,to);e[eid].v=to;e[eid].val=val;e[eid].next=p[from];p[from]=eid++;
}
int n,m;//n為點數 m為邊數
int h[MAXN];
int gap[MAXN];
int s,t;
int dfs(int pos,int cost)
{if (pos==t) return cost;int j,minh=n-1,lv=cost,d;for (j=p[pos];j!=-1;j=e[j].next){int v=e[j].v,val=e[j].val;if(val>0){if (h[v]+1==h[pos]){if (lv<e[j].val) d=lv;else d=e[j].val;d=dfs(v,d);e[j].val-=d;e[j^1].val+=d;lv-=d;if (h[s]>=n) return cost-lv;if (lv==0) break;}if (h[v]<minh)    minh=h[v];}}if (lv==cost){--gap[h[pos]];if (gap[h[pos]]==0) h[s]=n;h[pos]=minh+1;++gap[h[pos]];}return cost-lv;
}
int isap(int st,int ed)
{s=st;t=ed;int ret=0;memset(gap,0,sizeof(gap));memset(h,0,sizeof(h));gap[st]=n;while (h[st]<n)ret+=dfs(st,INT_MAX);return ret;
}
int main()
{while(cin>>m>>n){init();for(int i=0;i<m;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);insert1(u,v,c);}printf("%d\n",isap(1,n));}return 0;
}
View Code

?

補充:sap、isap(sap+gap優化)、dinic算法:

http://blog.csdn.net/sprintfwater/article/details/7913181

sap算法:

http://www.cnblogs.com/longdouhzt/archive/2011/09/04/2166187.html

轉載于:https://www.cnblogs.com/pdev/p/3873789.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273643.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273643.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273643.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

delphi讀取excel

簡單的例子 1 procedure TForm1.Button1Click(Sender: TObject);2 var3 ExcelApp,MyWorkBook: OLEVariant;4 begin5 opendialog1.Filter:Microsoft Excel Workbook (*.xls)|*.XLS|; 6 edit2.Text : sheet1;7 if opendialog1.Execute then8 begin9 edit1.Text:o…

Docker-compose 常用命令及網絡設置(五)

Docker Compose 常用命令 build 構建或重新構建服務。服務被構建后將會以 project_service的形式標記,例如:comoretest db。help 査看指定命令的幫助文檔,該命令非常實用。 docker-compose所有命令的幫助文檔都可通過該命令查看。 docker-compose he lp COMMAND 示例 docker-co…

淺談 trie樹 及其實現

定義&#xff1a;又稱字典樹&#xff0c;單詞查找樹或者前綴樹&#xff0c;是一種用于快速檢索的多叉樹結構&#xff0c; 如英文字母的字典樹是一個26叉樹&#xff0c;數字的字典樹是一個10叉樹。 核心思想&#xff1a;是空間換時間.利用字符串的公共前綴來降低查詢時間的開銷以…

Docker-compose 安裝與基本使用(四)

安裝 Docker-Compose Compose有多種安裝方式,例如通過 shell, pip以及將 Compose作為容器安裝等。本次安裝以Shell 為主。 通過以下命令自動下載并安裝適應系統版本的 Compose: curl -L "https://github.com/docker/compose/releases/download/1.10.0/docker-compose-$(un…

如何開始DDD(完)

連續寫了兩篇文章&#xff0c;這一篇我想是序的完結篇了。結合用戶注冊的例子再將他簡單豐富一下。在這里只添加一個簡單需求&#xff0c;就是用戶注冊成功后給用戶發一封郵件。補充一下之前的代碼 public class DomainService {public void Register(User user){if (_userRepo…

git pull 報錯:Untracked Fles Preventing Merge

場景 使用 git pull 命令更新報錯解決 找到對應的文件刪除后重新打開項目。

關于string,我今天科普的

今天下午朋友討論組上討論一個關于string的問題&#xff0c;問題是這樣的&#xff0c;string a"aaa";string ba;a"bbb",為什么測試b的值不改變&#xff1f;之前我看過一個文章&#xff0c;知道肯定不相等&#xff0c;因為引用地址的一系列問題&#xff0c;…

git pull 報錯:The following untracked working tree files would be overwritten by merge

場景 使用 git pull 命令更新報錯 Updating d652d1c..fa05549 error: The following untracked working tree files would be overwritten by merge:.idea/encodings.xmlPlease move or remove them before you can merge. Aborting 解決 使用 git clean -d -fx 命令即可。

SpringBoot 配置多數據源

項目Git地址&#xff1a;SpringBoot 配置多數據源&#xff1a;Jacob-multi-data-source 準備工作 準備兩個數據庫(此模塊中兩個數據庫一個為本地 一個為遠程&#xff0c;本地為主&#xff0c;遠程為從)。然后建表。 #本地庫 CREATE TABLE username (id bigint(11) NOT NULL AUT…

HDU 2912

直線關于球的多次反射&#xff0c;求最后一次反射點 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std; const double inf1e10; const double eps1e-8; struct point {doub…

EMVTag系列3《持卡人基本信息數據》

9F61 持卡人證件號 L&#xff1a;2–26 R&#xff08;需求&#xff09;&#xff1a;數據應存在&#xff0c;在讀應用數據過程中&#xff0c;終端不檢查&#xff1b; (PBOC2.0第五部分中規定)芯片中持卡人姓名 5F20與持卡人姓名擴展9F0B只能使用一個&#xff0c;另一個必須不…

BindingException: Parameter 'XXX' not found. Available parameters are [collection, list]

應業務需求&#xff0c;需要使用到MQ進行數據上傳和下發。傳遞格式為JSON,服務那邊下發JSON數組&#xff0c;接收端將JSON數組轉換成List集合&#xff0c;調用Mybatis-plus批量添加saveBatch()。提示字段未找到... org.apache.ibatis.exceptions.PersistenceException: ### Er…

JDK 8 新特性 之 default關鍵字

前言 Jdk1.8之前的接口中只聲明方法&#xff0c;方法具體實現應在子類中進行。Jdk1.8打破了這樣的用法&#xff1a;接口中可以實現具體的方法體&#xff0c;只需要加上關鍵字static或者default修飾即可。 default關鍵字 public interface UserService {//自定義方法void getUse…

headroom.js插件使用方法

1.什么是headroom.js&#xff1f; headroom是用純Javascript寫的插件&#xff0c;用來隱藏和展示頁面元素&#xff0c;從而為頁面留下更多空間。比如使用headroom能使導航欄當頁面下滾時消失&#xff0c;當頁面上滾時候又出現。&#xff08;查看效果&#xff09; 2.工作原理 通…

JDK 8 新特性 之 方法引用

概述 方法引用&#xff1a;當要傳遞給Lambda體的操作&#xff0c;已經有實現的方法了&#xff0c;就可以使用方法引用方法引用&#xff1a;在Lambda的基礎上進一步的簡化。換句話說&#xff0c;方法引用就是Lambda表達式&#xff0c;也就是函數式接口的一個實例&#xff0c;通過…

項目記錄:springmvc forward redirect 問題

RequestMapping("/redirect")public String redirect(RedirectAttributes redirectAttributes){redirectAttributes.addFlashAttribute("test", "testdata"); //專供此種情況下使用。return "redirect:read";} 注意&#xff1a;此種情…

JDK 8 新特性 之 Lambda表達式

前言 Lambda 表達式&#xff0c;也可稱為閉包&#xff0c;它是推動 Java 8 發布的最重要新特性。Lambda 允許把函數作為參數傳遞進方法中。使用 Lambda 表達式可以使代碼變的更加簡潔緊湊。lambda表達式的重要特征: 可選類型聲明&#xff1a;不需要聲明參數類型&#xff0c;編譯…

開源組件DocX導出Word

1、使用Docx替換Word模板里書簽里內容的一個方法 using Novacode;public class ExportWord{/// <summary>/// 導出word/// </summary>/// <param name"lBookMarks">書簽數據源</param>/// <param name"sTemplatePath">導出W…

JDK 8 新特性 之 Strams簡單使用

概述 Java 8 API添加了一個新的抽象稱為流Stream&#xff0c;可以讓你以一種聲明的方式處理數據。 Stream 使用一種類似用 SQL 語句從數據庫查詢數據的直觀方式來提供一種對 Java 集合運算和表達的高階抽象。 Stream API可以極大提供Java程序員的生產力&#xff0c;讓程序員寫出…

Cannot open include file: jni.h: No such file or directory解決方法

在此運行Visual Studio 2012 項目時出現 #include <stdio.h> #include <jni.h> int main() { printf("Hello World"); } But when I try to build, I get the following error - 1>c:testtest.cpp(2) : fatal error C1083: Cannot open include file:…