# 【解题报告】网络流24题

1.飞行员配对方案问题

1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #define LL long long
5 using namespace std;
6 const int N=105;
7 const int inf=0x3f3f3f3f;
8 int n,m,x,y,S,T,cnt=1,ans;
9 int cur[N],first[N],dis[N],q[N];
10 struct edge{int to,next,flow;}e[N*N*4];
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 void insert(int u,int v,int w)
19 {
20     e[++cnt]=(edge){v,first[u],w};first[u]=cnt;
21     e[++cnt]=(edge){u,first[v],0};first[v]=cnt;
22 }
23 bool bfs()
24 {
25     memset(dis,-1,sizeof(dis));
29     {
31         for(int i=first[u];i;i=e[i].next)
32         {
33             int v=e[i].to;
34             if(dis[v]!=-1||!e[i].flow)continue;
35             dis[v]=dis[u]+1;
36             q[tail++]=v;
37         }
38     }
39     return dis[T]!=-1;
40 }
41 int dfs(int u,int a)
42 {
43     if(u==T||a==0)return a;
44     int f,flow=0;
45     for(int& i=cur[u];i;i=e[i].next)
46     {
47         int v=e[i].to;
48         if(dis[v]==dis[u]+1&&(f=dfs(v,min(e[i].flow,a)))>0)
49         {
50             e[i].flow-=f;e[i^1].flow+=f;
51             flow+=f;a-=f;if(a==0)break;
52         }
53     }
54     return flow;
55 }
56 int main()
57 {
59     while(scanf("%d%d",&x,&y)==2)insert(x,y,1);
60     for(int i=1;i<=m;i++)insert(S,i,1);
61     for(int i=m+1;i<=n;i++)insert(i,T,1);
62     while(bfs())
63     {
64         for(int i=S;i<=T;i++)cur[i]=first[i];
65         ans+=dfs(S,inf);
66     }
67     printf("%d\n",ans);
68     return 0;
69 }
View Code

2.太空飞行计划问题

1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #define LL long long
5 using namespace std;
6 const int N=105;
7 const int inf=0x3f3f3f3f;
8 int n,m,x,S,T,cnt=1,ans,sum,num;
9 int cur[N],first[N],dis[N],q[N];
10 bool vis[N];
11 char c;
12 struct edge{int to,next,flow;}e[N*N*2];
14 {
15     int x=0,f=1;char c=getchar();
16     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
17     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
18     return x*f;
19 }
20 void insert(int u,int v,int w)
21 {
22     e[++cnt]=(edge){v,first[u],w};first[u]=cnt;
23     e[++cnt]=(edge){u,first[v],0};first[v]=cnt;
24 }
25 bool bfs()
26 {
27     memset(dis,-1,sizeof(dis));
31     {
33         for(int i=first[u];i;i=e[i].next)
34         {
35             int v=e[i].to;
36             if(dis[v]!=-1||!e[i].flow)continue;
37             dis[v]=dis[u]+1;
38             q[tail++]=v;
39         }
40     }
41     return dis[T]!=-1;
42 }
43 int dfs(int u,int a)
44 {
45     if(u==T||a==0)return a;
46     int f,flow=0;
47     for(int& i=cur[u];i;i=e[i].next)
48     {
49         int v=e[i].to;
50         if(dis[v]==dis[u]+1&&(f=dfs(v,min(e[i].flow,a)))>0)
51         {
52             e[i].flow-=f;e[i^1].flow+=f;
53             flow+=f;a-=f;if(a==0)break;
54         }
55     }
56     return flow;
57 }
58 void find(int x)
59 {
60     vis[x]=true;
61     for(int i=first[x];i;i=e[i].next)
62         if(!vis[e[i].to]&&e[i].flow)
63             find(e[i].to);
64 }
65 int main()
66 {
68     for(int i=1;i<=n;i++)
69     {
71         while(1)
72         {
73             num=0;c=getchar();
74             while(c<'0'||c>'9')c=getchar();
75             while(c>='0'&&c<='9')num=num*10+c-'0',c=getchar();
76             insert(i,num+n,inf);
77             if(c=='\r')break;
78         }
79     }
81     while(bfs())
82     {
83         for(int i=S;i<=T;i++)cur[i]=first[i];
84         ans+=dfs(S,inf);
85     }
86     find(S);
87     for(int i=1;i<=n;i++)if(vis[i])printf("%d ",i);
88     printf("\n");
89     for(int i=n+1;i<=n+m;i++)if(vis[i])printf("%d ",i-n);
90     printf("\n");
91     printf("%d\n",sum-ans);
92     return 0;
93 }
View Code

3.最小路径覆盖问题

1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<cmath>
5 #define LL long long
6 using namespace std;
7 const int N=1e4+5;
8 const int base=5e3;
9 const int inf=0x3f3f3f3f;
10 int n,m,x,y,S,T,cnt=1,ans,sum;
11 int cur[N],first[N],dis[N],q[N],to[N];
12 bool vis[N];
13 struct edge{int to,next,flow;}e[N*20];
15 {
16     int x=0,f=1;char c=getchar();
17     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
18     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
19     return x*f;
20 }
21 void insert(int u,int v,int w)
22 {
23     e[++cnt]=(edge){v,first[u],w};first[u]=cnt;
24     e[++cnt]=(edge){u,first[v],0};first[v]=cnt;
25 }
26 bool bfs()
27 {
28     memset(dis,-1,sizeof(dis));
32     {
34         for(int i=first[u];i;i=e[i].next)
35         {
36             int v=e[i].to;
37             if(dis[v]!=-1||!e[i].flow)continue;
38             dis[v]=dis[u]+1;
39             q[tail++]=v;
40         }
41     }
42     return dis[T]!=-1;
43 }
44 int dfs(int u,int a)
45 {
46     if(u==T||a==0)return a;
47     int f,flow=0;
48     for(int& i=cur[u];i;i=e[i].next)
49     {
50         int v=e[i].to;
51         if(dis[v]==dis[u]+1&&(f=dfs(v,min(e[i].flow,a)))>0)
52         {
53             e[i].flow-=f;e[i^1].flow+=f;
54             flow+=f;a-=f;if(a==0)break;
55         }
56     }
57     return flow;
58 }
59 bool ok(int x)
60 {
61     int t=(int)(sqrt(x)+1e-9);
62     return t*t==x;
63 }
64 int main()
65 {
67     while(1)
68     {
69         ans++;sum++;
70         for(int i=1;i<ans;i++)
71             if(ok(i+ans))insert(i,ans+base,1);
72         insert(S,ans,1);insert(ans+base,T,1);
73         while(bfs())
74         {
75             for(int i=S;i<=T;i++)cur[i]=first[i];
76             sum-=dfs(S,inf);
77         }
78         if(sum>n)break;
79     }
80     ans--;printf("%d\n",ans);
81     for(int x=1;x<=ans;x++)
82         for(int i=first[x];i;i=e[i].next)
83             if(!e[i].flow&&e[i].to)
84                 {to[x]=e[i].to-base;break;}
85     for(int x=1;x<=ans;x++)
86         if(!vis[x])
87         {
88             for(int i=x;i;i=to[i])
89                 printf("%d ",i),vis[i]=true;
90             printf("\n");
91         }
92     return 0;
93 }
View Code

5.圆桌问题

1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<cmath>
5 #define LL long long
6 using namespace std;
7 const int N=505;
8 const int inf=0x3f3f3f3f;
9 int n,m,x,S,T,cnt=1,ans,sum;
10 int cur[N],first[N],dis[N],q[N];
11 struct edge{int to,next,flow;}e[N*N*2];
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void insert(int u,int v,int w)
20 {
21     e[++cnt]=(edge){v,first[u],w};first[u]=cnt;
22     e[++cnt]=(edge){u,first[v],0};first[v]=cnt;
23 }
24 bool bfs()
25 {
26     memset(dis,-1,sizeof(dis));
30     {
32         for(int i=first[u];i;i=e[i].next)
33         {
34             int v=e[i].to;
35             if(dis[v]!=-1||!e[i].flow)continue;
36             dis[v]=dis[u]+1;
37             q[tail++]=v;
38         }
39     }
40     return dis[T]!=-1;
41 }
42 int dfs(int u,int a)
43 {
44     if(u==T||a==0)return a;
45     int f,flow=0;
46     for(int& i=cur[u];i;i=e[i].next)
47     {
48         int v=e[i].to;
49         if(dis[v]==dis[u]+1&&(f=dfs(v,min(e[i].flow,a)))>0)
50         {
51             e[i].flow-=f;e[i^1].flow+=f;
52             flow+=f;a-=f;if(a==0)break;
53         }
54     }
55     return flow;
56 }
57 int main()
58 {
62     for(int i=1;i<=n;i++)
63         for(int j=1;j<=m;j++)
64             insert(i,j+n,1);
65     while(bfs())
66     {
67         for(int i=S;i<=T;i++)cur[i]=first[i];
68         ans+=dfs(S,inf);
69     }
70     if(ans<sum){printf("0\n");return 0;}
71     printf("1\n");
72     for(int i=1;i<=n;i++,printf("\n"))
73         for(int j=first[i];j;j=e[j].next)
74             if(!e[j].flow&&e[j].to)printf("%d ",e[j].to-n);
75     return 0;
76 }
View Code

6.最长递增子序列问题

12.软件补丁问题

16.数字梯形问题

17.运输问题

18.分配工作问题

19.负载平衡问题

22.最大 k 可重线段集问题

24.骑士共存问题

posted @ 2018-04-23 14:58  Zsnuo  阅读(1124)  评论(0编辑  收藏  举报