Dinic算法学习&&HDU2063
http://www.cnblogs.com/SYCstudio/p/7260613.html
看这篇博文懂了一点,做题再体会体会吧
找了好久都没找到一个好用的模板……
我也是佛了。。最后决定用峰神的板子!
贴一个题解,HDU2063
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 using namespace std; 20 typedef struct Edge { 21 int u, v, c, next; 22 }Edge; 23 const int inf = 0x7f7f7f7f; 24 const int maxn = 10000; 25 #define maxm 30000 26 #define ll long long 27 #define mem(a,b) memset(a,b,sizeof a) 28 inline int read() 29 { 30 int x=0,f=1; 31 char ch=getchar(); 32 while(ch<'0'||ch>'9') 33 { 34 if(ch=='-') f=-1; 35 ch=getchar(); 36 } 37 while(ch>='0'&&ch<='9') 38 { 39 x=10*x+ch-'0'; 40 ch=getchar(); 41 } 42 return x*f; 43 } 44 int n,m,s,t,maxflow,tot=1,head[maxn],cur[maxn],h[maxn],q[maxn]; 45 struct edge{int go,next,v;}e[maxm]; 46 void ins(int x,int y,int z){e[++tot].go=y;e[tot].v=z;e[tot].next=head[x];head[x]=tot;} 47 void insert(int x,int y,int z){ins(x,y,z);ins(y,x,0);} 48 bool bfs() 49 { 50 mem(h,-1); 51 int l=0,r=1;q[1]=s;h[s]=0; 52 while(l<r) 53 { 54 int x=q[++l]; 55 for(int i=head[x];i;i=e[i].next) 56 if(e[i].v&&h[e[i].go]==-1) 57 { 58 h[e[i].go]=h[x]+1;q[++r]=e[i].go; 59 } 60 } 61 return h[t]!=-1; 62 } 63 int dfs(int x,int f) 64 { 65 if(x==t) return f; 66 int tmp,used=0; 67 for(int i=head[x];i;i=e[i].next) 68 if(e[i].v&&h[e[i].go]==h[x]+1) 69 { 70 tmp=dfs(e[i].go,min(e[i].v,f-used)); 71 e[i].v-=tmp;if(e[i].v)cur[x]=i; 72 e[i^1].v+=tmp;used+=tmp; 73 if(used==f)return f; 74 } 75 if(!used) h[x]=-1; 76 return used; 77 } 78 void dinic() 79 { 80 maxflow=0; 81 while(bfs()) 82 { 83 for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf); 84 } 85 } 86 void init() 87 { 88 mem(head,0); 89 mem(h,-1); 90 } 91 int main() { 92 int k; 93 while(~scanf("%d",&k)&&k){ 94 init(); 95 m=read();n=read(); 96 //源点到每个女生,每个男生到终点。 97 int src=0,des=m+m+n+1; 98 for(int i=1;i<=m;++i) 99 insert(src,i,1); 100 for(int i=1;i<=n;++i) 101 insert(m+i,des,1); 102 for(int i=1;i<=k;++i) 103 { 104 int a=read(),b=read(); 105 insert(a,b+m,1); 106 } 107 s=0;t=des; 108 dinic(); 109 cout<<maxflow<<endl; 110 } 111 return 0; 112 }