bzoj2561 最小生成樹 最小割
阿新 • • 發佈:2019-01-23
這道題目的做法有點神奇啊。。居然是用網路流。資料範圍20w有點嚇人啊。表示這種題目根本想不到是網路流,但告訴你是最小割還是恍然大悟的。
以最小生成樹為例。把所有小於L的邊取出來,顯然這些邊不能連通u和v,否則將u和v連起來在環上去掉L顯然更小。所以求一個最小割就行了。最大生成樹同理,兩者相加就是答案。
然而網路流20w顯然恐怖啊,後來翻lrj的書寫的dinic對於容量為1的網路時間複雜度為O(min(N^(2/3),M^(1/2))*M),對於100%看為O(M^1.5),而且還是一個比較鬆的上界應該沒什麼問題。
下附AC程式碼:
#include<iostream> #include<cstdio> #include<cstring> #define N 400005 #define inf 1000000000 using namespace std; int n,m,tot,sta,gol,fst[N],pnt[N],len[N],nxt[N],h[N],d[N]; struct node{ int x,y,z; }edg[N]; int read(){ int x=0; char ch=getchar(); while (ch<'0' || ch>'9') ch=getchar(); while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x; } void add(int aa,int bb,int cc){ pnt[++tot]=bb; len[tot]=cc; nxt[tot]=fst[aa]; fst[aa]=tot; } bool bfs(){ int head=0,tail=1; h[1]=sta; memset(d,-1,sizeof(d)); d[sta]=1; while (head!=tail){ int x=h[++head],p; for (p=fst[x]; p; p=nxt[p]) if (len[p]){ int y=pnt[p]; if (d[y]==-1){ d[y]=d[x]+1; h[++tail]=y; } } } return d[gol]!=-1; } int dinic(int x,int rst){ if (x==gol || !rst) return rst; int p,flow=0; for (p=fst[x]; p; p=nxt[p]) if (len[p]){ int y=pnt[p]; if (d[x]+1!=d[y]) continue; int tmp=dinic(y,min(rst,len[p])); if (!tmp) continue; rst-=tmp; flow+=tmp; len[p]-=tmp; len[p^1]+=tmp; if (!rst) break; } return flow; } int main(){ n=read(); m=read(); int i; for (i=1; i<=m; i++){ edg[i].x=read(); edg[i].y=read(); edg[i].z=read(); } sta=read(); gol=read(); int dvl=read(); tot=1; for (i=1; i<=m; i++) if (edg[i].z<dvl){ add(edg[i].x,edg[i].y,1); add(edg[i].y,edg[i].x,1); } int ans=0; while (bfs()) ans+=dinic(sta,inf); tot=1; memset(fst,0,sizeof(fst)); for (i=1; i<=m; i++) if (edg[i].z>dvl){ add(edg[i].x,edg[i].y,1); add(edg[i].y,edg[i].x,1); } while (bfs()) ans+=dinic(sta,inf); printf("%d\n",ans); return 0; }
by lych
2015.12.1