标签:following while oid using include i know img situation memset
妖怪题目,做到现在:2017/8/19 - 1:41……
不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√
题目链接:http://poj.org/problem?id=1815
Time Limit: 2000MS Memory Limit: 20000K
Description
Input
Output
Sample Input
3 1 3 1 1 0 1 1 1 0 1 1
Sample Output
1 2
总的来说,就是求最小点割集,做法参考:
http://www.cnblogs.com/lochan/p/3870697.html
http://wugj03.blog.163.com/blog/static/1737650582011219115316710/
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define in(x) x 5 #define out(x) x+n 6 #define MAX 500 7 #define INF 0x3f3f3f3f 8 using namespace std; 9 struct Dinic{ 10 int s,t,nv;//源点、汇点、点总数 11 int c[MAX][MAX],f[MAX][MAX],lev[MAX]; 12 bool vis[MAX]; 13 void addedge(int from,int to,int cap) 14 { 15 c[from][to]=cap, f[from][to]=0; 16 c[to][from]=0, f[to][from]=0; 17 } 18 bool bfs() 19 { 20 memset(vis,0,sizeof(vis)); 21 queue<int> q; 22 q.push(s); 23 vis[s]=1; 24 lev[s]=0; 25 while(!q.empty()) 26 { 27 int u=q.front();q.pop(); 28 for(int v=1;v<=nv;v++) 29 { 30 if(!vis[v] && c[u][v]>f[u][v])//属于残存网络的边 31 { 32 lev[v]=lev[u]+1; 33 q.push(v); 34 vis[v]=1; 35 } 36 } 37 38 } 39 return vis[t]; 40 } 41 int dfs(int u,int aug) 42 { 43 if(u==t) return aug; 44 int res=aug,tmp; 45 for(int v=1;v<=nv;v++) 46 { 47 if(lev[v]==lev[u]+1 && c[u][v]>f[u][v]) 48 { 49 tmp=dfs(v,min(aug,c[u][v]-f[u][v])); 50 f[u][v]+=tmp; 51 f[v][u]-=tmp; 52 aug-=tmp; 53 } 54 } 55 return res-aug; 56 } 57 int maxflow() 58 { 59 int res=0; 60 while(bfs()) res+=dfs(s,INF); 61 return res; 62 } 63 }dinic; 64 65 int n,S,T; 66 int main(){ 67 int a; 68 scanf("%d%d%d",&n,&S,&T); 69 dinic.nv=n*2, dinic.s=out(S), dinic.t=in(T); 70 for(int i=1;i<=n;++i) 71 { 72 if(i!=dinic.s && i!=dinic.t) dinic.addedge(in(i),out(i),1); 73 for(int j=1,tmp;j<=n;j++) 74 { 75 scanf("%d",&tmp); 76 if(i!=j && tmp) dinic.addedge(out(i),in(j),INF); 77 } 78 } 79 if(dinic.c[dinic.s][dinic.t]){ 80 puts("NO ANSWER!\n"); 81 return 0; 82 } 83 int ans=dinic.maxflow(); 84 printf("%d\n",ans); 85 86 for(int i=1;i<=n && ans;i++) 87 { 88 if(i==dinic.s|| i==dinic.t || !dinic.f[in(i)][out(i)]) continue; 89 memset(dinic.f,0,sizeof(dinic.f)); 90 dinic.c[in(i)][out(i)]=0; 91 if(dinic.maxflow()<ans) 92 { 93 ans--; 94 printf("%d ",i); 95 } 96 else dinic.c[in(i)][out(i)]=1; 97 } 98 printf("\n"); 99 return 0; 100 }
PS.为了方便后续使用该模板,把它也封装在一个struct里了,1~63行为模板。
POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
标签:following while oid using include i know img situation memset
原文地址:http://www.cnblogs.com/dilthey/p/7393012.html