【新人向】POJ3041——一水达成

来源:互联网 发布:淘宝亲密付可以提现吗 编辑:程序博客网 时间:2024/05/29 17:05

第一滴水,二分图裸题

考完期末考试感觉自己好渣……ubuntu害我连连CE,后来发现是评测器YW

#include <cstdio>#include <memory.h>#include <iostream>#include <iomanip>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>using namespace std;#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define red(i,a,b) for(int i=(a);i>=(b);i--)#define ll long longconst int maxn=600,maxm=10100;const int inf=2000000000;struct edge{int from,to,cap,flow;};vector <int> g[maxn*4];vector <edge> e;bool vis[maxn*4];int d[maxn*4],cur[maxn*4];int xx[maxm],yy[maxm];int n,m,s,t;void addedge(int from,int to,int cap){edge a,b;a.from=from;a.to=to;a.cap=cap;a.flow=0;b.from=to;b.to=from;b.cap=0;b.flow=0;e.push_back(a);e.push_back(b);int h=e.size();g[from].push_back(h-2);g[to].push_back(h-1);}bool bfs(int s,int t){memset(vis,0,sizeof(vis));queue<int> q;q.push(s);vis[s]=1;d[s]=0;while(!q.empty()){int x=q.front();q.pop();for(int i=0;i<g[x].size();i++){edge& ee=e[g[x][i]];if (!vis[ee.to] && ee.cap>ee.flow){vis[ee.to]=1;d[ee.to]=d[x]+1;q.push(ee.to);}}}return vis[t];}int dfs(int x,int a){if (x==t || a==0) return a;int flow=0,f;for(int& i=cur[x];i<g[x].size();i++){edge& ee=e[g[x][i]];if ( d[x]+1==d[ee.to] && (f=dfs(ee.to,min(ee.cap-ee.flow,a)))>0 ){ee.flow+=f;e[g[x][i]^1].flow-=f;flow+=f;a-=f;if (a==0) break;}}return flow;}int dinic(int s,int t){int flow=0;while(bfs(s,t)){memset(cur,0,sizeof(cur));flow+=dfs(s,inf);}return flow;}int main(){scanf("%d%d",&n,&m);rep(i,1,m) scanf("%d%d",&xx[i],&yy[i]);s=0;t=n*2+1;rep(i,1,m) addedge(xx[i],n+yy[i],1);rep(i,1,n) addedge(s,i,1);rep(i,1,n) addedge(n+i,t,1);int ans=dinic(s,t);printf("%d\n",ans);return 0;}


0 0
原创粉丝点击