题意:点之间有双向的联系,问去掉多少个点可以使s,t之间没有联系,点尽量少,序号尽量小。
思路: 网络流建图真难啊 , 看别人的思路过的 ,,
把一个点拆分成两个,出点和入点,对于有联系的点对,建立一条从s的出点到t 的入点的边 边权为无穷。
对每一个点,建立自己入点到出点权值为1的边。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> using namespace std; const int N = 409; const int INF = 0x3; int g[N][N],gap[N],pre[N],cur[N],dist[N],g1[N][N]; int n,m,s1,t1; int sap() { int ret = 0,aug=INF,u,v; memset(gap,0,sizeof(gap)); memset(dist,0,sizeof(dist)); for(int i=0;i<=n;i++) cur[i] = 1; u = pre[s1] = s1; gap[0] = n; while(dist[s1]<=n) { loop: for(v = cur[u];v<=n;v++) if(g[u][v]>0&&dist[u]==dist[v]+1) { cur[u]=v; aug = min(aug,g[u][v]); pre[v] = u; u =v; if(v==t1) { ret+=aug; for(u=pre[u];v!=s1;v=u,u=pre[u]) { g[u][v]-=aug;g[v][u]+=aug; } aug = INF; } goto loop; } int mind = n; for(v=1;v<=n;v++) if(g[u][v]>0&&mind>dist[v]) mind= dist[v],cur[u]=v; if(--gap[dist[u]]<=0) break; gap[dist[u] = mind+1]++; u = pre[u]; } return ret; } int main() { freopen("in.txt","r",stdin); int s,t,tmp; scanf("%d%d%d",&n,&s,&t); if(s>t) swap(s,t); s1 = s+n,t1 =t; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&tmp); if(i==j) continue; if(tmp) g1[i+n][j] = INF; } } if(g1[s+n][t]) { printf("NO ANSWER!\n"); return 0; } for(int i=1;i<=n;i++) g1[i][i+n] = 1; g1[s][s+n] = g1[t][t+n]=INF; m=n; n = n*2; memcpy(g,g1,sizeof(g1)); int a = sap(); int ans[N],cnt=0; for(int i=1;i<=m;i++) { if(i==s||i==t) continue; memcpy(g,g1,sizeof(g1)); g[i][m+i] = 0; int b = sap(); if(a!=b) { ans[cnt++]=i; g1[i][m+i] = 0; a=b; } if(b==0) break; } printf("%d\n",cnt); for(int i=0;i<cnt;i++) { if(i) printf(" "); printf("%d",ans[i]); }printf("\n"); return 0; }