poj 1815 Friendship 最小割输出最小方案
来源:互联网 发布:linux下svn命令 编辑:程序博客网 时间:2024/05/16 08:14
这题卡了好久啊,最小割模型很容易想,拆点就行。就像poj的Thieves一样
每个点 a拆成 a->a',容量为1。
其他相连的点 a'->b ,容量为INF
源连接s',t连接汇
问题在于输出最小的割集
更好的方法我还不会,只能枚举。
这里基于贪心的思想,从小到大删边,
若删除i->i',会使得最小割变小,则输出i,并且i->i'这条边不要恢复
若最小割不变,则恢复这条边,继续枚举。
一开始就是因为恢复了要割去的边,无限WA。
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<set>#include<map>#include<queue>#include<vector>#include<string>#define eps 1e-12#define INF 0x7fffffff#define maxn 22222using namespace std;int n,m;int en;int st,ed,s,t;//源点和汇点int dis[maxn] ;//dis[i],表示 到 原点 s 的 层数int que[9999999];bool delet[maxn];struct edge{int u,to,c,next;};edge e[999999];edge save[999999];int head[maxn];void add(int a,int b,int c){e[en].to=b;e[en].c=c;e[en].next=head[a];head[a]=en++;e[en].to=a;e[en].c=0;e[en].next=head[b];head[b]=en++;}int bfs(){ memset(dis,-1,sizeof(dis)); dis[st]=0; int front=0,rear=0; que[rear++]=st; while(front<rear) { int j=que[front++]; for(int k=head[j];k!=-1;k=e[k].next) { int i=e[k].to;if(dis[i]==-1&&e[k].c) { dis[i] = dis[j]+ 1 ; que[rear++]=i; if(i==ed) return true; } } } return false;}int dfs(int x,int mx){ int i,a; if(x==ed) return mx ; int ret=0; for(int k=head[x];k!=-1&&ret<mx;k=e[k].next) { if(e[k].c&&dis[e[k].to]==dis[x]+1) { int dd=dfs(e[k].to,min(e[k].c,mx-ret)); e[k].c-=dd; e[k^1].c+=dd; ret+=dd; } } if(!ret) dis[x]=-1; return ret;}bool del[maxn];int a[205][205];void init(){st=0; //源 ed=n+n+1; //汇for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } memset(del,0,sizeof(del));}void pr(int a,int b,int c){ printf("%d --> %d =%d \n",a,b,c);}void build(){ en=0; memset(head,-1,sizeof(head)); add(st,s+n,INF); for(int i=1;i<=n;i++) { if(!del[i]) { add(i,i+n,1); } for(int j=1;j<=n;j++) { if(a[i][j]) { add(i+n,j,INF); } } } add(t,ed,INF);}int dinic(){ int tmp=0; int maxflow=0; while(bfs()) { while(tmp=dfs(st,INF)) maxflow+=tmp; } return maxflow;}int main(){ while(scanf("%d%d%d",&n,&s,&t)!=EOF) { init(); build(); if(a[s][t]) {puts("NO ANSWER!");continue;} int ans=dinic(); printf("%d\n",ans); for(int i=1;i<=n;i++) { if(i==s||i==t) continue; del[i]=1; build(); int tmp=dinic(); if(tmp<ans) { if(ans-1) printf("%d ",i); else printf("%d",i); ans--; } else del[i]=0; if(ans==0) break; } puts(""); }}/*3 1 31 1 01 1 10 1 110 2 10 0 1 1 1 0 1 1 1 00 0 1 1 0 0 0 1 0 11 1 0 1 0 1 0 1 0 11 1 1 0 0 0 1 0 0 11 0 0 0 0 0 1 0 1 00 0 1 0 0 0 0 0 0 11 0 0 1 1 0 0 1 0 11 1 1 0 0 0 1 0 0 11 0 0 0 1 0 0 0 0 10 1 1 1 0 1 1 1 1 010 10 10 1 1 1 1 0 1 1 0 01 0 0 0 0 1 1 1 1 11 0 0 1 1 1 1 1 1 11 0 1 0 1 1 0 1 1 01 0 1 1 0 0 1 1 1 10 1 1 1 0 0 1 0 0 11 1 1 0 1 1 0 1 1 01 1 1 1 1 0 1 0 0 00 1 1 1 1 0 1 0 0 10 1 1 0 1 1 0 0 1 0*/
4 0
- poj 1815 Friendship 最小割输出最小方案
- POJ 1815 Friendship 最小割 + 字典序输出割点
- poj 1815 Friendship //最小割
- POJ 1815 Friendship 最小割
- POJ 1815 Friendship(最小割)
- POJ 1815 Friendship 最小割
- POJ 1815 Friendship 最小割
- POJ 1815 Friendship | 最小割、最小点割集
- POJ 1815 Friendship (最小点割集/最小割)
- POJ 1815 Friendship 最小割+字典序最小割集
- POJ 1815 Friendship(最小割)
- 【最小割+枚举】POJ-1815 Friendship
- POJ 1815 - Friendship(网络流‘最小割)
- POJ 1815 Friendship(最小割+拆点法)
- poj 1815 Friendship(最小割,求割集)
- poj 1815 Friendship (最小割 (贪心 最小割点集))
- POJ 1815 Friendship(字典序最小最小割)
- poj 1815 Friendship 求字典序最小的最小割
- 解决maven运行site:site的时候出现org/sonatype/aether/graph/DependencyFilter ClassNotFound的问题
- nodejs v0.10.26 安装express
- NHibernate扩展方法(-) --NHibernateHelper
- GoldenGate复制3:异构跨平台跨版本支持DDL的oracle复制
- hdu题目分类
- poj 1815 Friendship 最小割输出最小方案
- Best Time to Buy and Sell Stock 2
- POJ 3254 状压DP
- 文件系统概述
- JS 计算2个日期相差的天数
- Redis02-SDS字符串表示
- getCacheDir()和getFilesDir()方法区别
- SOCKET编程之select
- NHibernate扩展方法(二) --查询分页