POJ-3155-Hard Life(最大密度子图)(01分数规划+最小割)
2017-04-19 21:17
531 查看
链接:http://poj.org/problem?id=3155
求最大密度子图,
见论文:算法合集之《最小割模型在信息学竞赛中的应用》
P20-26
求最大密度子图,
见论文:算法合集之《最小割模型在信息学竞赛中的应用》
P20-26
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstring> #include <cstdio> #include <algorithm> #include <iostream> #include <queue> #include <cmath> #include <string> #include <map> #include <stack> using namespace std; #define INF 0x3f3f3f3f #define MAXN 5007 #define MAXM 5007 int n,m; const int mod=499999; #define eps 1e-8 int x[MAXN],y[MAXN],used[MAXN],vis[MAXN],du[MAXN],cnt; double z[MAXN]; int deep[MAXN]; struct node { int v,next; double flow; } edge[MAXM]; int ind, head[MAXN]; void add_(int u, int v, double flow) { edge[ind].v=v; edge[ind].flow=flow; edge[ind].next=head[u]; head[u]=ind++; edge[ind].v=u; edge[ind].flow=0; edge[ind].next=head[v]; head[v]=ind++; } bool BFS(int S, int T) { memset(deep,0,sizeof(deep)); deep[S]=1; queue<int> q; q.push(S); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u]; i+1; i=edge[i].next) { int v= edge[i].v; if(!deep[v]&&edge[i].flow>=eps) { deep[v]=deep[u]+1; q.push(v); if(v==T)return 1; } } } return 0; } double DFS(int S, int T, double max_) { if(S==T)return max_; double ans=0,f; for(int i=head[S]; i+1; i=edge[i].next) { int v=edge[i].v; if(deep[S]+1==deep[v] && edge[i].flow>eps) { f=DFS(v,T,min(max_-ans,edge[i].flow)); ans+=f; edge[i].flow-=f; edge[i^1].flow+=f; } } if(eps<=ans)return ans; deep[S]=0; return 0; } double Dinic(int S, int T) { double ans=0,t; while(BFS(S,T)) { while( ( t=DFS(S,T,INF) ) >=eps) ans+=t; } return ans; } void D(int u) { vis[u]=1; if(u>0&&u<=n)cnt++; for(int i=head[u]; i+1; i=edge[i].next) { int v=edge[i].v; if(!vis[v] && edge[i].flow>eps)D(v); } } void build(double p) { ind=0; memset(head,-1,sizeof(head)); for(int i=0; i<m; ++i) { add_(x[i],y[i],1); add_(y[i],x[i],1); } for(int i=1; i<=n; ++i) { add_(0,i,m); add_(i,n+1,m*1.0+2.0*p-du[i]*1.0); } } int main() { int cp=0; while(scanf("%d%d",&n,&m)!=EOF) { if(m==0) { printf("1\n1\n"); continue ; } memset(du,0,sizeof(du)); for(int i=0; i<m; ++i) { scanf("%d%d",&x[i],&y[i]); du[ x[i] ]++; du[ y[i] ]++; } double l=0.0,r=m,ans,mid; while(r-l>=1.0/n/n) { mid=(l+r)/2.0; ans=0; build(mid); double temp=Dinic(0,n+1); ans=(n*m*1.0-temp)/2.0; if(ans<eps)r=mid; else l=mid; } cnt=0; build(l); Dinic(0,n+1); memset(vis,0,sizeof(vis)); D(0); printf("%d\n",cnt); for(int i=1; i<=n; ++i) if(vis[i]) printf("%d\n",i); } return 0; } /* 5 6 1 5 5 4 4 2 2 5 1 2 3 1 5 3 1 3 1 2 2 3 5 5 1 2 1 3 2 3 1 5 1 4 1.5 15 0.75 13.5 1.125 15 0.9375 14.625 1.03125 15 0.984375 14.9062 1.00781 15 */
相关文章推荐
- poj 3155 Hard Life(最大密度子图,01分数规划)
- poj 3155 Hard Life(01分数规划+最大流--最大密度子图)
- poj 3155 Hard Life 【最大密度子图】 【0-1分数规划 + 最小割】
- 最大密集子图(01分数规划+二分+最小割)POJ3155
- poj 3155(Hard Life)分数规划/最大密度子图
- 最大密集子图(01分数规划+二分+最小割)POJ3155
- poj 3155 01规划->最大密度密度子图->最大流
- POJ 3155 Hard Life(最小割 最大密度子图)
- 【01分数规划】最大密度子图
- poj 3155 Hard Life 分数规划+最大权闭合图
- poj 3155 Hard Life (最大密度子图)
- poj 3155 Hard Life 最大密度子图
- poj 3155 Hard Life (最大密度子图)
- POJ 3155 最大密度子图 二分+最小割
- POJ 3155 Hard Life 最大密度子图
- 【POJ】3155 Hard Life 最大密度子图
- POJ 3621 Sightseeing Cows 最大密度环 01分数规划
- POJ 3155 Hard Life(最大密度子图)
- POJ 3155 Hard Life 最大密度子图
- poj 3155 Hard Life 分数规划+最大权闭合图