标签:des style class blog code color
Description
Input
Output
Sample Input
3 1 3 1 1 0 1 1 1 0 1 1
Sample Output
1 2
求无向图最小割点集,转化成最小割边来求,主要是构图,还要拆点
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; int Map[200+10][200+10],g[200+10][200+10]; int N,S,T; bool vist[200+10]; struct Edge { int from,to,cap,flow; }; struct DINIC{ static const int maxn=2500+10; int n,m,s,t; vector<Edge>edges; vector<int>G[maxn]; int d[maxn],cur[maxn]; bool vis[maxn]; void AddEdge(int from,int to,int cap) { Edge temp; temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to; edges.push_back(temp); temp.cap=0; temp.flow=0; temp.from=to; temp.to=from; edges.push_back(temp); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { memset(vis,0,sizeof(vis)); queue<int>Q; Q.push(s); d[s]=0; vis[s]=1; while(!Q.empty()) { int x=Q.front();Q.pop(); for (int i=0;i<G[x].size();i++) { Edge& e=edges[G[x][i]]; if (!vis[e.to] && e.cap>e.flow) { vis[e.to]=1; d[e.to]=d[x]+1; Q.push(e.to); } } } return vis[t]; } int DFS(int x,int a) { if (x==t || a==0) return a; int flow=0,f; for (int& i=cur[x];i<G[x].size();i++) { Edge& e=edges[G[x][i]]; if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0) { e.flow+=f; edges[G[x][i]^1].flow-=f; flow+=f; a-=f; if (a==0) break; } } return flow; } int Dinic() { int flow=0; while (BFS()) { memset(cur,0,sizeof(cur)); flow+=DFS(s,inf); } return flow; } void init() { for (int i=0;i<=maxn;i++) G[i].clear(); edges.clear(); } }; DINIC friends; void build() { friends.init(); for (int i=1;i<=N;i++) { if (i==S || i==T) { friends.AddEdge(i,i+N,inf); } else friends.AddEdge(i,i+N,1); for (int j=1;j<=N;j++) { if (Map[i][j] && i!=j) friends.AddEdge(i+N,j,inf); } } } int main() { //freopen("in.txt","r",stdin); while(scanf("%d%d%d",&N,&S,&T)!=EOF) { friends.s=S; friends.t=T+N; friends.n=2*N; for (int i=1;i<=N;i++) for (int j=1;j<=N;j++){ scanf("%d",&Map[i][j]); } if (Map[S][T]==1) { printf("NO ANSWER!\n"); continue; } build(); int ans=friends.Dinic(); printf("%d\n",ans); memset(vist,0,sizeof(vist)); for (int i=1;i<=N;i++) { if (i==S || i==T) continue; for (int j=1;j<=N;j++) for (int k=1;k<=N;k++) { g[j][k]=Map[j][k]; if (j==i || k==i) Map[j][k]=0; } build(); int ans1=friends.Dinic(); if (ans1<ans) { vist[i]=true; ans--; } else { for (int j=1;j<=N;j++) for (int k=1;k<=N;k++) Map[j][k]=g[j][k]; } } int k=1; for (int i=1;i<=N;i++) { if (vist[i]) { if (k==1) {printf("%d",i);k++;} else printf(" %d",i); } } printf("\n"); } return 0; }
标签:des style class blog code color
原文地址:http://www.cnblogs.com/chensunrise/p/3785702.html