BZOJ 2561 最小生成樹

2022-09-19 08:27:07 字數 1774 閱讀 4409

第一眼瞎那啥貪心,然後覺得不太對勁,就滾去看題解,發現是網路流otz

模擬kruskal的過程發現,若要在最小生成樹**現,權值則小於的邊不能讓u,v聯通,轉換成最小割模型,最大生成樹同理。

跑兩遍最大流。

注意邊要建雙向的啊,被這點坑死了。然後板子不要瞎那啥亂打。

順便發現果然isap跑得很快。

//

twenty

#include#include

#include

#include

#include

#include

#include

#include

#define inf 0x3f3f3f3ftypedef

long

long

ll;using

namespace

std;

const

int maxn=1e6+299

;int ecnt=1

,ans,n,m,u,v,w,fir[maxn],pre[maxn];

struct

edge

edge(

int u,int v,int

w):u(u),v(v),w(w){}

}ee[maxn];

struct

edge

edge(

intfrom,int to,int cap,int flow,int nxt):from(from

),to(to),cap(cap),flow(flow),nxt(nxt){}

}e[maxn];

intd[maxn],c[maxn],cur[maxn];

void add(int

from,int to,int

cap)

queue

que;

void bfs(int s,int

t) }

}}int cal(int s,int

t)

return

fl;}

int maxflow(int s,int

t)

int ok=0

;

for(int &i=cur[x];i;i=e[i].nxt)

if(d[e[i].to]+1==d[x]&&e[i].cap>e[i].flow)

if(!ok)

if(!(--c[d[x]])) break

; c[d[x]=m]++;

if(x!=s) x=e[pre[x]].from

; }

}return

res;

}int

main()

scanf(

"%d%d%d

",&u,&v,&w);

for(int i=1;i<=m;i++)

}ans+=maxflow(u,v);

memset(fir,

0,sizeof

(fir));

memset(pre,

0,sizeof

(pre));

ecnt=1

;

for(int i=1;i<=m;i++)

}ans+=maxflow(u,v);

printf(

"%d\n

",ans);

return0;

}

view code

bzoj 2561 最小生成樹

給定乙個邊帶正權的連通無向圖g v,e 其中n v m e n個點從1到n依次編號,給定三個正整數u,v,和l u v 假設現在加入一條邊權為l的邊 u,v 那麼需要刪掉最少多少條邊,才能夠使得這條邊既可能出現在最小生成樹上,也可能出現在最大生成樹上?第一行包含用空格隔開的兩個整數,分別為n和m 接...

bzoj2561 最小生成樹

time limit 10 sec memory limit 128 mb submit 1024 solved 520 submit status discuss 給定乙個邊帶正權的連通無向圖g v,e 其中n v m e n個點從1到n依次編號,給定三個正整數u,v,和l u v 假設現在加入一...

bzoj 2561 最小生成樹

給定乙個邊帶正權的連通無向圖,現在加入一條邊權為l的邊 u,v 那麼需要刪掉最少多少條邊,才能夠使得這條邊既可能出現在最小生成樹上,也可能出現在最大生成樹上?以前看著一臉懵逼,現在好像就是那樣。容易想到,當u v存在一條路徑,上面不存在 l的邊,那麼新邊一定不在最小生成樹上,所以將所有小於l的邊建出...