瞎DP一下就好了 不过是搞得我蛮昏的
我懒 找一篇题解就当做我写过题解了
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
inline char nc(){
static char buf[100000],*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 int N=100005;
struct edge{
int u,v,w,next;
}G[N<<1];
int head[N],inum;
inline void add(int u,int v,int w,int p){
G[p].u=u; G[p].v=v; G[p].w=w; G[p].next=head[u]; head[u]=p;
}
#define V G[p].v
int fat[N],depth[N];
int path[N],tot,cir[N],dis[N],faw[N];
inline void find(int u,int fa){
fat[u]=fa; depth[u]=depth[fa]+1;
for (int p=head[u];p;p=G[p].next)
if (V!=fa)
if (!depth[V])
faw[V]=G[p].w,find(V,u);
else if (depth[V]<depth[u]){
int t=u;
while (t!=V) path[tot]=t,cir[t]=1,dis[tot++]=faw[t],t=fat[t];
path[tot]=V,cir[V]=1,dis[tot++]=G[p].w;
}
}
double f[N],g[N];
int son[N];
inline void dfs1(int u,int fa){
f[u]=0; son[u]=0;
for (int p=head[u];p;p=G[p].next)
if (V!=fa && !cir[V])
dfs1(V,u),son[u]++,f[u]+=f[V]+G[p].w;
if (son[u]) f[u]/=son[u];
}
int n,m;
inline void dfs2(int u,int fa){
fat[u]=fa;
if (fa){
if (cir[fa]) g[u]=2*g[fa]; else g[u]=g[fa];
int tmp=0;
for (int p=head[fa];p;p=G[p].next)
if (V!=fat[fa] && !cir[V])
if (V!=u)
g[u]+=G[p].w+f[V];
else
tmp=G[p].w;
if (n==m)
if (cir[fa])
g[u]=g[u]/(son[fa]+1)+tmp;
else
g[u]=g[u]/son[fa]+tmp;
else
if (!fat[fa])
g[u]=g[u]/(son[fa]-1)+tmp;
else
g[u]=g[u]/son[fa]+tmp;
}
for (int p=head[u];p;p=G[p].next)
if (V!=fa && !cir[V])
dfs2(V,u);
}
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),add(iu,iv,iw,++inum),add(iv,iu,iw,++inum);
if (m==n-1){
dfs1(1,0);
dfs2(1,0);
double ans=0;
for (int i=1;i<=n;i++)
if (i==1)
ans+=f[i];
else
ans+=(son[i]*f[i]+g[i])/(son[i]+1);
printf("%.5lf\n",ans/n);
}else{
find(1,0);
for (int i=0;i<tot;i++)
dfs1(path[i],0);
for (int i=0;i<tot;i++){
double tmp=0,p,sum;
p=1.0/2; sum=0;
for (int j=1;j<tot;j++){
int k=(i+j)%tot;
sum+=dis[(k+tot-1)%tot];
if (son[path[k]] && j!=tot-1)
tmp+=(sum+f[path[k]])*p*son[path[k]]/(son[path[k]]+1);
if (j==tot-1)
tmp+=(sum+f[path[k]])*p;
p=p/(son[path[k]]+1);
}
p=1.0/2; sum=0;
for (int j=1;j<tot;j++){
int k=(i+tot-j)%tot;
sum+=dis[k];
if (son[path[k]] && j!=tot-1)
tmp+=(sum+f[path[k]])*p*son[path[k]]/(son[path[k]]+1);
if (j==tot-1)
tmp+=(sum+f[path[k]])*p;
p=p/(son[path[k]]+1);
}
g[path[i]]=tmp;
}
for (int i=0;i<tot;i++)
dfs2(path[i],0);
double ans=0;
for (int i=1;i<=n;i++)
if (cir[i])
ans+=(son[i]*f[i]+2*g[i])/(son[i]+2);
else
ans+=(son[i]*f[i]+g[i])/(son[i]+1);
printf("%.5lf\n",ans/n);
}
return 0;
}