最大密度子图的建图 详见论文
这个东西是支持强制选的
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef double ld;
inline char nc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const ld eps=1e-7;
namespace D{
const int N=505;
const int M=N*N*2;
struct edge{
int u,v,next; ld f;
}G[M<<1];
int head[N],inum=1;
inline void add(int u,int v,ld f,int p){
G[p].u=u; G[p].v=v; G[p].f=f; G[p].next=head[u]; head[u]=p;
}
inline void link(int u,int v,ld f){
add(u,v,f,++inum); add(v,u,0,++inum);
}
#define V G[p].v
int S,T;
int Q[N],l,r,dis[N];
inline bool bfs(){
for (int i=1;i<=T;i++) dis[i]=-1;
l=r=-1; Q[++r]=S; dis[S]=0;
while (l<r){
int u=Q[++l];
for (int p=head[u];p;p=G[p].next)
if (G[p].f>eps && dis[V]==-1){
dis[V]=dis[u]+1; Q[++r]=V;
if (V==T) return 1;
}
}
return 0;
}
int cur[N];
inline ld dfs(int u,ld flow){
if (u==T) return flow;
ld used=0;
for (int p=cur[u];p;p=G[p].next){
cur[u]=p;
if (G[p].f>eps && dis[V]==dis[u]+1){
ld d=dfs(V,min(G[p].f,flow-used));
G[p].f-=d; G[p^1].f+=d;
used+=d; if (fabs(flow-used)<eps) break;
}
}
if (used<eps) dis[u]=-1;
return used;
}
inline ld Dinic(){
ld ans=0;
while (bfs()){
memcpy(cur,head,sizeof(int)*(T+5));
ans+=dfs(S,1e10);
}
return ans;
}
}
const int N=405;
int n,m;
int w[N][N]; ld d[N];
int a[N];
const int U=400*(100+2000);
inline bool check(ld g){
using namespace D; cl(head); inum=1;
S=n+1,T=n+2;
for (int i=1;i<=n;i++){
d[i]=0;
for (int j=1;j<=n;j++)
if (i^j)
d[i]+=w[i][j]+2*g;
}
ld ret=0;
for (int i=1;i<=n;i++)
if (!a[i]){
link(S,i,U);
link(i,T,U+2.0*(2*n-1)*g-d[i]);
}else{
link(S,i,1e10);
ret+=U+2.0*(2*n-1)*g-d[i];
}
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
link(i,j,w[i][j]+2*g),link(j,i,w[i][j]+2*g);
ret+=Dinic();
ret-=U*n;
ret=-ret;
return ret>=-eps;
}
int main(){
int iu,iv,iw;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(m);
for (int i=1;i<=m;i++)
read(iu),read(iv),read(iw),w[iu][iv]=w[iv][iu]=iw;
for (int i=1;i<=n;i++) read(a[i]);
ld L=0,R=400,MID;
while (R-L>eps)
if (check(MID=(L+R)/2))
L=MID;
else
R=MID;
printf("%.6lf\n",(L+R)/2);
return 0;
}
一开始写的最大权闭合子图被卡了
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef double ld;
inline char nc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const ld eps=1e-7;
namespace D{
const int N=100005;
const int M=N<<2;
struct edge{
int u,v,next; ld f;
}G[M<<1];
int head[N],inum=1;
inline void add(int u,int v,ld f,int p){
G[p].u=u; G[p].v=v; G[p].f=f; G[p].next=head[u]; head[u]=p;
}
inline void link(int u,int v,ld f){
add(u,v,f,++inum); add(v,u,0,++inum);
}
#define V G[p].v
int S,T;
int Q[N],l,r,dis[N];
inline bool bfs(){
for (int i=1;i<=T;i++) dis[i]=-1;
l=r=-1; Q[++r]=S; dis[S]=0;
while (l<r){
int u=Q[++l];
for (int p=head[u];p;p=G[p].next)
if (G[p].f>eps && dis[V]==-1){
dis[V]=dis[u]+1; Q[++r]=V;
if (V==T) return 1;
}
}
return 0;
}
int cur[N];
inline ld dfs(int u,ld flow){
if (u==T) return flow;
ld used=0;
for (int p=cur[u];p;p=G[p].next){
cur[u]=p;
if (G[p].f>eps && dis[V]==dis[u]+1){
ld d=dfs(V,min(G[p].f,flow-used));
G[p].f-=d; G[p^1].f+=d;
used+=d; if (fabs(flow-used)<eps) break;
}
}
if (used<eps) dis[u]=-1;
return used;
}
inline ld Dinic(){
ld ans=0;
while (bfs()){
memcpy(cur,head,sizeof(int)*(T+5));
ans+=dfs(S,1e10);
}
return ans;
}
}
const int N=405;
int n,m;
int w[N][N];
int a[N];
inline bool check(ld ans){
using namespace D; cl(head); inum=1;
S=n+n*(n-1)/2+1; T=n+n*(n-1)/2+2;
int cur=0; ld ret=0;
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++){
++cur;
link(S,n+cur,w[i][j]+2*ans);
if (!a[i]) link(n+cur,i,1e8);
if (!a[j]) link(n+cur,j,1e8);
ret+=w[i][j]+2*ans;
}
int c=0;
for (int i=1;i<=n;i++)
if (!a[i])
link(i,T,ans*(2*n-1));
else
ret+=-ans*(2*n-1);
ret-=Dinic();
return ret>=-eps;
}
int main(){
int iu,iv,iw;
freopen("t.in","r",stdin);
freopen("t1.out","w",stdout);
read(n); read(m);
for (int i=1;i<=m;i++)
read(iu),read(iv),read(iw),w[iu][iv]=w[iv][iu]=iw;
for (int i=1;i<=n;i++) read(a[i]);
ld L=0,R=1e3,MID;
while (R-L>eps)
if (check(MID=(L+R)/2))
L=MID;
else
R=MID;
printf("%.6lf\n",(L+R)/2);
return 0;
}