【整合】網路流ISAP演算法模板,整合isap演算法模板

來源:互聯網
上載者:User

【整合】網路流ISAP演算法模板,整合isap演算法模板

注釋應該比較清楚了吧= =
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 2048#define MAXINT 0x7fffffffusing namespace std;int n,m;//點的數目和邊的數目int dis[MAXN+1];//距離標號 int num[MAXN+1];int src,des;//src源點,des匯點 int u,v,w;struct edge{int f;//某條弧當前流量 int c;//容量int ver;//弧的起點edge *rev;//反弧 edge *next;//弧的終點 edge(){};edge(int v,int cap,edge *nex):ver(v),c(cap),next(nex),rev(NULL),f(0){};void* operator new(size_t, void *p){ return p; }}*s[MAXN+1];void init(){int queue[MAXN+1],head=0,tail=0;for (int i=1;i<=n;i++)    dis[i]=MAXN,num[i]=0;queue[tail++]=des;dis[des]=0;num[0]=1;while (head!=tail){int v=queue[head++];edge *e=s[v];while (e){if ((e->rev)&&(e->rev->c==0)||dis[e->ver]<MAXN) {}//若改點後的邊不可通過//流或者已經更新就無需更新else {dis[e->ver]=dis[v]+1;++num[dis[e->ver]];queue[tail++]=e->ver;}e=e->next;}}}int maxflow()//ISAP演算法實現過程 {int st=src,ret=0;edge *E[MAXN],*rep[MAXN];for (int i=1;i<=n;i++) E[i]=s[i];while (dis[src]<n)//不存在增廣路徑就停止 {if (st==des)//找到了增廣路徑 {int delta=MAXINT;for (int i=src;i!=des;i=E[i]->ver)delta=min(delta,E[i]->c);for (int i=src;i!=des;i=E[i]->ver){E[i]->c-=delta;E[i]->f+=delta;E[i]->rev->c+=delta;E[i]->rev->f-=delta;}ret+=delta;st=src;}edge *e;for (e=E[st];e;e=e->next)if (e->c>0&&dis[st]==dis[e->ver]+1) break;if (e)//存在允許弧 {E[st]=e;rep[e->ver]=e->rev;st=e->ver;}else{if ((--num[dis[st]])==0) break;//GAP最佳化 E[st]=s[st];int mind=n;for (edge *t=s[st];t;t=t->next)if (t->c>0)    mind=min(mind,dis[t->ver]);dis[st]=mind+1;++num[dis[st]];if (st!=src) st=rep[st]->ver;}    }return ret;}int main(){freopen("ditch.in","r",stdin);freopen("ditch.out","w",stdout);scanf("%d%d",&m,&n);src=1,des=n;edge *buffer=new edge[2*m]; edge *data=buffer;while (m--){scanf("%d%d%d",&u,&v,&w);s[u]=new((void*) data++) edge(v,w,s[u]);s[v]=new((void*) data++) edge(u,0,s[v]);s[u]->rev=s[v];s[v]->rev=s[u];}init();printf("%d",maxflow());}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.