網路流 預流推進模板

2021-09-28 16:04:56 字數 4877 閱讀 8177

模板鏈結

這裡特別注意!!!第乙個題解有致命漏洞!雖然可以過模板,但是大部分題re!!這裡改正:

在模板hlpp中,原題解:

if(!

--gap[h[now]

])

應改為:

if

(h[now]

!= inf &&

!--gap[h[now]

])

更新(2020.5.14):下面這個已經不可以過洛谷的模板了, 所以追(piao)加(qie)了個更快的.

下面是模板**:

const

int n =

1e5+5;

const

int inf =

0x3f3f3f3f

;int tot =1;

int ver[n<<1]

, w[n<<1]

, he[n]

, ne[n<<1]

;int h[n]

, e[n]

, gap[n<<1]

, inq[n]

;int n, m, k;

int s, t;

int all;

//all為最大節點編號,從0或1開始需要改遍歷範圍。

struct cmp};

queue<

int> q;

priority_queue<

int, vector<

int>

, cmp> p;

void

add(

int x,

int y,

int _w =1)

bool

bfs()}

return h[s]

!= inf;

}void

_push

(int now)}}

void

relabel

(int now)

}int

hlpp()

}while

(p.size()

)}return e[t]

;}

乙個更快的板子(按理說比賽的時候沒有出題人會卡的這麼死吧):

下面是模板**:

const

int inf =

0x7fffffff

;std::vector<

int>list[

1203

],h,cnt,que,e;

typedef std::list<

int> list;

std::vectoriter;

list dlist[

1203];

typedef std::vector

::iterator iterator;

int hst,nowh;

int he[

1203

], ver[

24000010

], ne[

2400010

], c[

2400010

], tot =1;

inline

void

addedge

(const

int x,

const

int y,

const

int w)

inline

void

relabel

(int n,

int t)}}

for(

register

int i=

0;i<=n;

++i)

for(

register

int u=

0;u++u)

} hst=

(nowh=h[que[qt-1]

]);}

inline

void

push

(int u,

int i)

inline

void

_push

(int n,

int u)

else nh=std::

min(nh,h[y]+1

);}}

int het=h[u];if

(cnt[het]==1

) dlist[i]

.clear()

;}hst=het-1;

}else

}inline

inthlpp

(int n,

int s,

int t)

u=list[nowh]

.back()

; list[nowh]

.pop_back()

;_push

(n,u);}

return e[t]

+inf;

}int n,m,s,t,u,v,f;

signed

main()

printf

("%d"

,hlpp

(n+1

,s,t));

return0;

}

乙個最快的板子,(下面的那個存圖用vector, 這兩個的都來著洛谷題解中的大佬, 上面那個是我重構的,俺也不知道為啥向前星慢,我也不是第一次遇見這種情況了 )

下面是模板**:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define ll long long

#define int ll

#define ull unsigned ll

#define _min(x, y) ((x)>(y)?(y):(x))

#define _max(x, y) ((x)>(y)?(x):(y))

#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )

#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )

#define pr(x, y) (make_pair((x), (y)))

//using namespace std;

const

int inf =

0x7fffffff

;struct edge };

std::vectora[

1203];

std::vector<

int>list[

1203

],h,cnt,que,e;

typedef std::list<

int> list;

std::vectoriter;

list dlist[

1203];

typedef std::vector

::iterator iterator;

int hst,nowh;

inline

void

addedge

(const

int u,

const

int v,

const

int f)

inline

void

relabel

(int n,

int t)}}

for(

register

int i=

0;i<=n;

++i)

for(

register

int u=

0;u++u)

} hst=

(nowh=h[que[qt-1]

]);}

inline

void

push

(int u,edge &ed)

inline

void

push

(int n,

int u)

else nh=std::

min(nh,h[p-

>to]+1

);}}

int het=h[u];if

(cnt[het]==1

) dlist[i]

.clear()

;}hst=het-1;

}else

}inline

inthlpp

(int n,

int s,

int t)

u=list[nowh]

.back()

; list[nowh]

.pop_back()

;push

(n,u);}

return e[t]

+inf;

}inline

intread()

while

(c>=

'0'&&c<=

'9')

return f*fu;

}int n,m,s,t,u,v,f;

signed

main()

printf

("%d"

,hlpp

(n+1

,s,t));

return0;

}

網路流 費用流模板

中求的是最小費用最大流,求最大費用最大流只需要在設定邊權時設定為原權值的相反數,執行一次最小費用最大流,計算得出最小費用的相反數就是要求的最大費用。u x v x w x c x 分別表示 x 號邊的出發點,到達點,權值和殘量。1 intspfa 29 dis s 0 10 head tail 1 ...

最高標號預流推進演算法

from queue import queue from queue import priorityqueue class graph def init self,s,t,m 源,匯,頂點個數 self.edge self.s s 源 self.t t 匯 self.num m 頂點個數 self....

網路流模板

鄰接矩陣 include include include using namespace std const int inf 1 30 const int point num 300 int cap point num point num dist point num gap point num 初...