标签:ring blog tchar min src idt string 文件 code
网络流 最大流
题目描述很明显了。源点连边到正飞行员,正飞行员连边到副飞行员,副飞行员连边到汇点,DINIC求最大流
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 const int mxn=300; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 13 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 14 return x*f; 15 } 16 struct edge{ 17 int v,nxt,f; 18 }e[mxn*mxn*2]; 19 int hd[mxn],mct=1; 20 void add_edge(int u,int v,int c){ 21 e[++mct].v=v;e[mct].f=c;e[mct].nxt=hd[u];hd[u]=mct;return; 22 } 23 void insert(int u,int v,int c){ 24 add_edge(u,v,c);add_edge(v,u,0);return; 25 } 26 int n,m,S,T; 27 int d[mxn]; 28 bool BFS(){ 29 memset(d,0,sizeof d); 30 d[S]=1; 31 queue<int>q; 32 q.push(S); 33 while(!q.empty()){ 34 int u=q.front();q.pop(); 35 for(int i=hd[u];i;i=e[i].nxt){ 36 int v=e[i].v; 37 if(!d[v] && e[i].f){ 38 d[v]=d[u]+1;q.push(v); 39 } 40 } 41 } 42 return d[T]; 43 } 44 int DFS(int u,int lim){ 45 if(u==T)return lim; 46 int tmp,f=0; 47 for(int i=hd[u];i;i=e[i].nxt){ 48 int v=e[i].v; 49 if(d[v]==d[u]+1 && e[i].f){ 50 tmp=DFS(v,min(lim,e[i].f)); 51 e[i].f-=tmp; 52 e[i^1].f+=tmp; 53 lim-=tmp; 54 f+=tmp; 55 if(!lim)return f; 56 } 57 } 58 d[u]=0; 59 return f; 60 } 61 int Dinic(){ 62 int res=0; 63 while(BFS())res+=DFS(S,1e9); 64 return res; 65 } 66 int main(){ 67 freopen("flyer.in","r",stdin); 68 freopen("flyer.out","w",stdout); 69 scanf("%d%d",&n,&m); 70 S=0;T=n+1; 71 int i,j; 72 for(i=1;i<=m;i++){insert(S,i,1);} 73 for(i=m+1;i<=n;i++){insert(i,T,1);} 74 int a,b; 75 while(scanf("%d%d",&a,&b)!=EOF){ 76 insert(a,b,1); 77 } 78 int ans=Dinic(); 79 printf("%d\n",ans); 80 return 0; 81 }
标签:ring blog tchar min src idt string 文件 code
原文地址:http://www.cnblogs.com/SilverNebula/p/6240213.html