【最大流】【Dinic】bzoj2929 [Poi1999]洞穴攀行
TMD 题意其实是与1或n相连的边只能走一次,其他可以走无限次……翻译去死。
裸最大流。
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define INF 2147483647 #define MAXN 211 #define MAXM 40301 int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM]; int d[MAXN],cur[MAXN]; queue<int>q; int n,S,T,m,To; void Init_Dinic(){memset(first,-1,sizeof(first)); en=0; S=1; T=n;} void AddEdge(const int &U,const int &V,const int &W) {v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++; v[en]=U; next[en]=first[V]; first[V]=en++;} bool bfs() { memset(d,-1,sizeof(d)); q.push(S); d[S]=0; while(!q.empty()) { int U=q.front(); q.pop(); for(int i=first[U];i!=-1;i=next[i]) if(d[v[i]]==-1 && cap[i]) { d[v[i]]=d[U]+1; q.push(v[i]); } } return d[T]!=-1; } int dfs(int U,int a) { if(U==T || !a) return a; int Flow=0,f; for(int &i=cur[U];i!=-1;i=next[i]) if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i])))) { cap[i]-=f; cap[i^1]+=f; Flow+=f; a-=f; if(!a) break; } if(!Flow) d[U]=-1; return Flow; } int max_flow() { int Flow=0,tmp=0; while(bfs()) { memcpy(cur,first,(n+5)*sizeof(int)); while(tmp=dfs(S,INF)) Flow+=tmp; } return Flow; } int main() { scanf("%d",&n); Init_Dinic(); for(int i=1;i<n;++i) { scanf("%d",&m); for(;m;--m) {scanf("%d",&To); AddEdge(i,To,(i==1||To==T) ? 1 : INF);} } printf("%d\n",max_flow()); }
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/