This question can be said to be a minimum cut entry-level question. If you can think of the minimum cut problem, the idea of creating a graph is a matter of course. Add a source vertex S and sink vertex T; connect S to each spy, with an infinite capacity; connect city N (I .e. the location of the airport) to the sink vertex T, with an infinite capacity; there is a road between the cities connected, the capacity is 1, note that here is a bidirectional edge; after creating the graph, according to the maximum flow minimum cut theorem, then directly find the maximum flow.
This question can be said to be a minimum cut entry-level question.
If you can think of the minimum cut problem, the idea of creating a graph is a matter of course.
Add a source vertex S and sink vertex T;
Connects S to each spyware with an infinite capacity;
Connect City N (the location of the airport) to the settlement point T, with an infinite capacity;
There is a road between the cities connected, the capacity is 1, note that this is a two-way edge;
After creating the graph, based on the maximum flow least cut theorem, you can directly find the maximum flow.
Let's talk less about the code:
#include
using namespace std;#include
#include
#include
#include
#include
#include
#include
#include
#include
#include#define INS 1<<30#define CLR(arr,v) memset(arr,v,sizeof(arr))#define MaxV 3000#define MaxE 100000class MaxFlow{public:void Clear(){CLR(h,-1); CLR(cnt,0); CLR(vis,0);flag = false;pos = top = head = total = maxflow = 0;}void add(int u,int v,int flow){num[pos] = v;sur[pos] = flow;next[pos] = h[u];h[u] = pos++;num[pos] = u;sur[pos] = 0;next[pos] = h[v];h[v] = pos++;}int GetMaxFlow(int s,int t){init(t);stk[top] = s;while(!flag){minres = INS;if(top < 0) top = 0;if(!dfs(stk[top],-1,t,minres)) continue;maxflow += minres;while(dis != -1){sur[dis] -= minres;sur[dis^1] += minres;dis = pre_e[dis];}top = 0;}return maxflow;}private:int h[MaxV],num[MaxE],sur[MaxE],next[MaxE],gap[MaxV],cnt[MaxV],pre_e[MaxE],stk[MaxV],que[MaxV];int pos,top,head,total,maxflow,minres,dis;bool vis[MaxV],flag;void init(int n){que[total++] = n;vis[n] = true;while(head < total){int p = que[head++];if(head >= MaxV) head -= MaxV;for(int i = h[p]; i != -1 ;i = next[i]){if(!vis[ num[i] ]){vis[ num[i] ] = true;gap[ num[i] ] = gap[p] + 1;cnt[ gap[ num[i] ] ]++;que[total++] = num[i];if(total >= MaxV) total -= MaxV;}}}}bool dfs(int p,int father,int n,int &minres){int m = minres;for(int i = h[p]; i != -1 ;i = next[i]){if(sur[i] > 0 && gap[p] - gap[ num[i] ] == 1){minres = min(minres,sur[i]);pre_e[i] = father;if(num[i] != n) stk[++top] = num[i];if(num[i] == n || dfs(num[i],i,n,minres)) {if(num[i] == n) dis = i;return true;}minres = m;}}cnt[ gap[p] ]--;cnt[ gap[p] + 1]++;top--;if(cnt[ gap[p] ] == 0) flag = true;gap[p] += 1;return false;}}T;int main(){ int t; scanf("%d",&t); int cnt = 0; while (t--) { int n,m,p; scanf("%d%d%d",&n,&m,&p); int N = n + 1; T.Clear(); int x; for (int i = 1; i <= p; ++ i) scanf("%d",&x),T.add(0,x,INS); for (int i = 1; i <= m; ++ i) { int u,v; scanf("%d%d",&u,&v); T.add(u,v,1); T.add(v,u,1); } T.add(n,N,INS); printf("Case #%d: %d\n",++cnt,T.GetMaxFlow(0,N)); } return 0;}