# 【刷题】BZOJ 4349 最小树形图

3
10.00 1
1.80 1
2.50 2
2
1 3 2.00
3 2 1.50

15.50

## Solution

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=50+5,MAXM=MAXN*MAXN;
const db inf=100000000000.00;
int n,m,times[MAXN],vis[MAXN],bel[MAXN],snt,s,pre[MAXN],nd,M[MAXN];
db in[MAXN],G[MAXN][MAXN],ans;
struct node{
int u,v;
db k;
};
node side[MAXM];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline db solve(int rt,int n)
{
db res=0;
while(true)
{
for(register int i=1;i<=n;++i)in[i]=inf;
for(register int i=1;i<=snt;++i)
if(side[i].u!=side[i].v&&in[side[i].v]>side[i].k)in[side[i].v]=side[i].k,pre[side[i].v]=side[i].u;
for(register int i=1;i<=n;++i)
if(i!=rt&&in[i]==inf)return -1;
int cnt=0;
memset(bel,0,sizeof(bel));
memset(vis,0,sizeof(vis));
in[rt]=0;
for(register int i=1,j;i<=n;++i)
{
res+=in[i];j=i;
while(j!=rt&&vis[j]!=i&&!bel[j])vis[j]=i,j=pre[j];
if(j!=rt&&!bel[j])
{
bel[j]=++cnt;
for(register int k=pre[j];k!=j;k=pre[k])bel[k]=cnt;
}
}
if(!cnt)break;
for(register int i=1;i<=n;++i)
if(!bel[i])bel[i]=++cnt;
for(register int i=1,u,v;i<=snt;++i)
{
u=side[i].u,v=side[i].v;
side[i].u=bel[u];side[i].v=bel[v];
if(bel[u]^bel[v])side[i].k-=in[v];
}
n=cnt;
rt=bel[rt];
}
return res;
}
int main()
{
s=++nd;
for(register int i=1;i<=n;++i)
{
in[i]=inf;
if(times[i])
{
M[i]=++nd;
side[++snt]=(node){s,M[i],cost};
chkmin(in[i],cost);
}
}
for(register int i=1;i<=m;++i)
{
db cost;scanf("%lf",&cost);
if(M[x]&&M[y])
{
side[++snt]=(node){M[x],M[y],cost};
chkmin(in[y],cost);
}
}
for(register int i=1;i<=n;++i)
if(times[i]>1)ans+=(times[i]-1)*in[i];
printf("%.2f\n",ans+solve(s,nd));
return 0;
}

posted @ 2018-07-09 20:05  HYJ_cnyali  阅读(245)  评论(0编辑  收藏  举报