码迷,mamicode.com
首页 > 其他好文 > 详细

Cogs 14. [网络流24题] 搭配飞行员

时间:2015-11-24 18:30:18      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

这道题其实蛮好想的,因为分为正,副飞行员。所以就把正飞行员当作Boy,副飞行员当作Girl。然后做Hungry即可。
技术分享
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int G[131][131],vis[131],girl[131],n,n1;
 4 int hungry(int boy)
 5 {
 6     int i;
 7     for(i=n1+1;i<=n;i++)
 8     {
 9         if(G[boy][i]==1&&vis[i]==0)
10         {
11             vis[i]=1;
12             if(hungry(girl[i])==1||girl[i]==0)
13             {
14                 girl[i]=boy;
15                 return 1;
16             }
17         }
18     }
19     return 0;
20 }
21 int main()
22 {
23     int a,b,sum,i;
24     scanf("%d %d",&n,&n1);
25     while(scanf("%d %d",&a,&b)!=EOF)G[a][b]=1;
26     memset(girl,0,sizeof(girl));
27     sum=0;
28     for(i=1;i<=n1;i++)
29     {
30         memset(vis,0,sizeof(vis));
31         if(hungry(i)==1)sum++;
32     }
33     printf("%d",sum); 
34     return 0;
35 }
View Code
但这道题是“网络流24题”,所以还是要练一练网络流。所以直接用Dinic做最大流即可。
技术分享
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<cstdlib>
 5 #include<algorithm> 
 6 using namespace std;
 7 int n,s,t,q[131],dis[131],G[131][131];
 8 int Min(int aaa,int bbb){if(aaa>bbb)return bbb;else return aaa;}
 9 int Bfs()
10 {
11     int head,tail,u,v;
12     memset(q,0,sizeof(q)),head=0,tail=1,q[tail]=s;
13     memset(dis,-1,sizeof(dis)),dis[s]=0;
14     while(head<=tail)
15     {
16         u=q[++head];
17         for(v=1;v<=n;v++)
18         {
19             if(G[u][v]>0&&dis[v]<0)
20             {
21                 dis[v]=dis[u]+1;
22                 q[++tail]=v;
23             }
24         }
25     }
26     if(dis[n]<=0)return 0;
27     else return 1;
28 }
29 int Dfs(int u,int minflow)
30 {
31     int v,ans;
32     if(u==n)return minflow;
33     for(v=1;v<=n;v++)
34     {
35         if(G[u][v]>0&&dis[v]==dis[u]+1)
36         {
37             ans=Dfs(v,Min(minflow,G[u][v]));
38             if(ans!=0)
39             {
40                 G[u][v]-=ans;
41                 G[v][u]+=ans;
42                 return ans;
43             }
44         } 
45     }
46     return 0;
47 }
48 int Dinic()
49 {
50     int maxflow=0,ans;
51     while(Bfs()!=0)
52     {
53         ans=Dfs(s,0x7fffffff);if(ans==0)break;maxflow+=ans;
54     }
55     return maxflow;
56 }
57 int main()
58 {
59     int n1,a,b,i;
60     scanf("%d %d",&n,&n1);
61     while(scanf("%d %d",&a,&b)!=EOF)G[a][b]=1;
62     n+=2;s=n-1;t=n;
63     for(i=1;i<=n1;i++)G[s][i]=1;
64     for(i=n1+1;i<=n-2;i++)G[i][t]=1;
65     printf("%d",Dinic());
66     return 0; 
67 }
View Code



Cogs 14. [网络流24题] 搭配飞行员

标签:

原文地址:http://www.cnblogs.com/wjcwjc/p/4992459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!