http://poj.org/problem?id=3084 (Topic link)
Test instructions
A house has m rooms, some rooms have a door connected, and the door switch is only in one room, that is to say, only one room can control the switch of the door. Now some of the rooms have intruders, asking for the minimum number of doors to be closed to protect room N.
Solution
At least the number of doors closed so that the N point does not connect with the intrusion point, it is easy to see the minimum cut model, so the map becomes obvious.
For a Door (U,V): From the U-line with a capacity of 1 side to V, indicating that the door can be closed by u, from the V-connected to a capacity of the INF to u, indicating that the door can not be closed by V. Then create a new meeting point T, from all the intrusion points to the edge of the T-link capacity INF. Next, run Dinic from N to T to find the smallest cut.
Details
Pay attention to the case of a special sentence without solution.
Code
poj3084#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #include <queue> #define LL long long#define inf 1000000000#define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int maxn= 10010;struct Edge {int to,next,w;} E[maxn];int head[maxn],d[maxn];int M,cnt,ans,es,et;char ch[10];void Init () {memset (head); head,0,sizeof ;} void link (int u,int v,int W) {e[++cnt]= (edge) {v,head[u],w};head[u]=cnt;} BOOL BFs () {memset (d,-1,sizeof (d));queue<int> Q;q.push (es);d [Es]=0;while (!q.empty ()) {int X=q.front (); Q.pop () ; for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {D[e[i].to]=d[x]+1;q.push (e[i].to);}} return d[et]>0;} int dfs (int x,int f) {if (X==et | | f==0) return f;int used=0,w;for (int i=head[x];i;i=e[i].next) if (E[I].W && d[e [I].to]==d[x]+1] {W=dfs (e[i].to,min (e[i].w,f-used)); used+=w;e[i].w-=w;e[I^1].w+=w;if (used==f) return used;} if (!used) D[x]=-1;return used;} BOOL Dinic () {while (BFS ()) {int Tmp=dfs (es,inf); if (Tmp==inf) return 0;else ans+=tmp;} return 1;} int main () {int t;scanf ("%d", &t), while (t--) {Init (); scanf ("%d%d", &m,&es); es++;et=m+1;for (int x,u=1;u <=m;u++) {scanf ("%s", ch), if (ch[0]== ' I ') Link (u,et,inf), Link (et,u,0), scanf ("%d", &x); for (int v,i=1;i<=x;i + +) {scanf ("%d", &v); V++;link (u,v,1); link (v,u,inf);}} if (Dinic ()) printf ("%d\n", ans), Else puts ("PANIC-BREACH");} return 0;}
"poj3084" Panic