【最小割】【Dinic】【強聯通分量縮點】bzoj1797 [Ahoi2009]Mincut 最小割

來源:互聯網
上載者:User

標籤:style   blog   io   color   sp   for   on   div   log   

結論:

滿足條件一:當一條邊的起點和終點不在 殘量網路的 一個強聯通分量中。且滿流。

滿足條件二:當一條邊的起點和終點分別在 S 和 T 的強聯通分量中。且滿流。、

網上題解很多的。

 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 #define INF 2147483647 8 #define MAXN 4011 9 #define MAXM 12010110 int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM];11 int d[MAXN],cur[MAXN],cmp[MAXN],sum;12 bool vis[MAXN];13 queue<int>q;14 vector<int>vs;15 int n,m,S,T,A,B,C;16 void Init_Dinic(){memset(first,-1,sizeof(first)); en=0;}17 void AddEdge(const int &U,const int &V,const int &W)18 {v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++;19 v[en]=U; next[en]=first[V]; first[V]=en++;}20 bool bfs()21 {22     memset(d,-1,sizeof(d)); q.push(S); d[S]=0;23     while(!q.empty())24       {25         int U=q.front(); q.pop();26         for(int i=first[U];i!=-1;i=next[i])27           if(d[v[i]]==-1 && cap[i])28             {29               d[v[i]]=d[U]+1;30               q.push(v[i]);31             }32       }33     return d[T]!=-1;34 }35 int dfs(int U,int a)36 {37     if(U==T || !a) return a;38     int Flow=0,f;39     for(int &i=cur[U];i!=-1;i=next[i])40       if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))41         {42           cap[i]-=f; cap[i^1]+=f;43           Flow+=f; a-=f; if(!a) break;44         }45     if(!Flow) d[U]=-1;46     return Flow;47 }48 void max_flow()49 {50     while(bfs())51       {52         memcpy(cur,first,(n+5)*sizeof(int));53         while(dfs(S,INF));54       }55 }56 void dfs(int U)57 {58     vis[U]=1;59     for(int i=first[U];i!=-1;i=next[i]) if(cap[i]&&(!vis[v[i]])) dfs(v[i]);60     vs.push_back(U);61 }62 void dfs2(int U)63 {64     cmp[U]=sum;65     for(int i=first[U];i!=-1;i=next[i]) if(cap[i^1]&&(!cmp[v[i]])) dfs2(v[i]);66 }67 void scc()68 {69     for(int i=1;i<=n;i++) if(!vis[i]) dfs(i);70     vector<int>::iterator it=vs.end(); --it;71     for(;;--it)72       {73         if(!cmp[*it]) {++sum; dfs2(*it);}74         if(it==vs.begin()) break;75       }76 }77 int main()78 {79     scanf("%d%d%d%d",&n,&m,&S,&T); Init_Dinic();80     for(;m;--m)81       {82           scanf("%d%d%d",&A,&B,&C);83           AddEdge(A,B,C);84       }85     max_flow(); scc();86     for(int i=0;i<en;i+=2)87       printf("%d %d\n",(!cap[i])&&cmp[v[i+1]]!=cmp[v[i]],(!cap[i])&&cmp[v[i+1]]==cmp[S]&&cmp[v[i]]==cmp[T]);88     return 0;89 }

【最小割】【Dinic】【強聯通分量縮點】bzoj1797 [Ahoi2009]Mincut 最小割

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.