1 /**************************************************************
2 Problem: 2878
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:556 ms
7 Memory:9876 kb
8 ****************************************************************/
9
10 //BZOJ 2878
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=1e5+10,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31 int to[N<<1],nxt[N<<1],head[N],cnt;
32 double l[N<<1];
33 void ins(int x,int y,double z){
34 to[++cnt]=y; nxt[cnt]=head[x]; head[x]=cnt; l[cnt]=z;
35 }
36
37 int n,m;
38
39 int vis[N],flag;
40 double son[N],fa[N],up[N],down[N];
41 int cir[N],tot,hash[N];
42 int pre[N],nex[N];
43 double len[25][25];
44
45 void Findcir(int x,int f){
46 vis[x]=1;
47 for(int i=head[x];i;i=nxt[i])
48 if (to[i]!=f){
49 if (vis[to[i]]){
50 flag=to[i];
51 return;
52 }
53 Findcir(to[i],x);
54 if (flag>0){
55 if (flag==x) flag=-1;
56 return;
57 }
58 if (flag==-1) break;
59 }
60 vis[x]=0;
61 }
62 void dfs_cir(int x,int f){
63 if (hash[x]) return;
64 cir[++tot]=x;
65 hash[x]=tot;
66 fa[x]=2;
67 for(int i=head[x];i;i=nxt[i]){
68 if (to[i]==f) continue;
69 if (!vis[to[i]]) continue;
70
71 pre[to[i]]=x;
72 nex[x]=to[i];
73 dfs_cir(to[i],x);
74 len[hash[x]][hash[to[i]]]=len[hash[to[i]]][hash[x]]=l[i];
75 break;
76 }
77 }
78
79 void dfsdown(int x,int f){
80 for(int i=head[x];i;i=nxt[i])
81 if (!vis[to[i]] && to[i]!=f){
82 fa[to[i]]=1;
83 dfsdown(to[i],x);
84 son[x]++;
85 down[x]+=down[to[i]]+l[i];
86 }
87 if (son[x]) down[x]/=son[x];
88 }
89
90 void dfsup(int x,int f,double ee){
91 up[x]=ee;
92 if (fa[f]+son[f]>1)
93 up[x]+=(fa[f]*up[f]+son[f]*down[f]-down[x]-ee)/(fa[f]+son[f]-1);
94 for(int i=head[x];i;i=nxt[i])
95 if (to[i]!=f) dfsup(to[i],x,l[i]);
96 }
97
98 int main(){
99 #ifndef ONLINE_JUDGE
100 freopen("2878.in","r",stdin);
101 freopen("2878.out","w",stdout);
102 #endif
103 n=getint(); m=getint();
104 F(i,1,m){
105 int x=getint(),y=getint(),z=getint();
106 ins(x,y,z); ins(y,x,z);
107 }
108 Findcir(1,0);
109 if (m<n){
110 dfsdown(1,0);
111 for(int i=head[1];i;i=nxt[i])
112 dfsup(to[i],1,l[i]);
113 }else{
114 F(i,1,n)
115 if (vis[i]){
116 dfs_cir(i,0);
117 break;
118 }
119 F(i,1,tot) dfsdown(cir[i],0);
120 F(i,1,tot){
121 int x=cir[i];
122 double k=1;
123 for(int j=nex[x];j!=x;j=nex[j]){
124 if (nex[j]!=x)
125 up[x]+=k*(len[hash[pre[j]]][hash[j]]+down[j]*son[j]/(son[j]+1));
126 else
127 up[x]+=k*(len[hash[pre[j]]][hash[j]]+down[j]);
128 k/=(son[j]+1);
129 }
130 k=1;
131 for(int j=pre[x];j!=x;j=pre[j]){
132 if (pre[j]!=x)
133 up[x]+=k*(len[hash[nex[j]]][hash[j]]+down[j]*son[j]/(son[j]+1));
134 else
135 up[x]+=k*(len[hash[nex[j]]][hash[j]]+down[j]);
136 k/=(son[j]+1);
137
138 }
139 up[x]/=2;
140 }
141 F(j,1,tot){
142 for(int i=head[cir[j]];i;i=nxt[i])
143 if (!hash[to[i]]) dfsup(to[i],cir[j],l[i]);
144 }
145 }
146 double ans=0;
147 F(i,1,n) ans+=(up[i]*fa[i]+down[i]*son[i])/(fa[i]+son[i]);
148 printf("%.5lf\n",ans/n);
149 return 0;
150 }