标签:close cstring ctime splay 二分图最大匹配 ems iostream mem cout
先复习一下dinic
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<ctime> 5 #include<iostream> 6 #include<algorithm> 7 #include<queue> 8 #include<set> 9 #define inf (0x7fffffff) 10 #define maxint (2147483647) 11 #define l(a) ((a)<<1) 12 #define r(a) ((a)<<1|1) 13 #define b(a) (1<<(a)) 14 #define rep(i,a,b) for(int i=a;i<=(b);i++) 15 #define clr(a) memset(a,0,sizeof(a)) 16 typedef long long ll; 17 using namespace std; 18 int readint(){ 19 int t=0,f=1;char c=getchar(); 20 while(!isdigit(c)){ 21 if(c==‘-‘) f=-1; 22 c=getchar(); 23 } 24 while(isdigit(c)){ 25 t=(t<<3)+(t<<1)+c-‘0‘; 26 c=getchar(); 27 } 28 return t*f; 29 } 30 ll readll(){ 31 ll t=0ll,f=1ll;char c=getchar(); 32 while(!isdigit(c)){ 33 if(c==‘-‘) f=-1ll; 34 c=getchar(); 35 } 36 while(isdigit(c)){ 37 t=(t<<3ll)+(t<<1ll)+ll(c-‘0‘); 38 c=getchar(); 39 } 40 return t*f; 41 } 42 const int maxn=5009,maxe=1000009; 43 int n,m,en,S,T,ans,d[maxn]; 44 struct edge{ 45 int v,w; 46 edge*next,*r; 47 inline edge(int V,int W){ 48 v=V;w=W; 49 } 50 inline edge(){}; 51 }E[maxe],*pt=E,*fir[maxn],*cur[maxn]; 52 void add(int u,int v,int w){ 53 pt->v=v;pt->w=w; 54 pt->next=fir[u];fir[u]=pt++; 55 } 56 void addedge(int u,int v,int w){ 57 add(u,v,w);add(v,u,0); 58 fir[u]->r=fir[v];fir[v]->r=fir[u]; 59 } 60 bool Bfs(){ 61 rep(i,S,T) d[i]=0; 62 queue<int>Q;Q.push(S);d[S]=1; 63 while(!Q.empty()){ 64 int x=Q.front();Q.pop(); 65 for(edge*e=fir[x];e;e=e->next) if(e->w&&!d[e->v]){ 66 d[e->v]=d[x]+1;Q.push(e->v); 67 } 68 } 69 return d[T]; 70 } 71 int Dfs(int x,int f){ 72 if(x==T||!f) return f; 73 int t=0; 74 for(edge*&e=cur[x];e;e=e->next) if(e->w&&d[e->v]==d[x]+1){ 75 int g=Dfs(e->v,min(f,e->w)); 76 if(g){ 77 e->w-=g;e->r->w+=g; 78 t+=g;f-=g; 79 if(!f) break; 80 } 81 } 82 return t; 83 } 84 void Dinic(){ 85 while(Bfs()){ 86 rep(i,S,T) cur[i]=fir[i]; 87 ans+=Dfs(S,inf); 88 } 89 } 90 int main(){ 91 //freopen("#input.txt","r",stdin); 92 //freopen("#output.txt","w",stdout); 93 n=readint();m=readint();en=readint();S=0;T=n+m+1; 94 rep(i,1,en){ 95 int U=readint(),V=readint(); 96 if(V>m) continue; 97 addedge(U,n+V,1); 98 } 99 rep(i,1,n) addedge(S,i,1); 100 rep(i,1,m) addedge(n+i,T,1); 101 Dinic(); 102 cout<<ans<<endl; 103 //fclose(stdin); 104 //fclose(stdout); 105 return 0; 106 }
匈牙利的坑待填
标签:close cstring ctime splay 二分图最大匹配 ems iostream mem cout
原文地址:http://www.cnblogs.com/chensiang/p/7850901.html