$NOI2012$迷失游乐园
作者:互联网
换根\(DP+\)基环树,思路不难,要考虑的东西很多,因为我直接在环上跑编号,所以细节更多。。。
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int f=1,w=0;char x=0;
while(x<'0'||x>'9') {if(x=='-') f=-1; x=getchar();}
while(x!=EOF&&x>='0'&&x<='9') {w=(w<<3)+(w<<1)+(x^48);x=getchar();}
return w*f;
}
const int N=100010,M=30;
pair<int,int> Cir[N];
int num_edge,n,m,Tim,Cnt;
int Id[N],Dfn[N],Nex[N],Tag[N];
double Up[N],Down[N],ans[N],Dis[M][M],Ans;
int head[N<<1],Son[N],Vis[N],fa[N];
struct Edge{int next,to,dis;} edge[N<<1];
inline void Add(int from,int to,int dis)
{
edge[++num_edge].next=head[from];
edge[num_edge].dis=dis;
edge[num_edge].to=to;
head[from]=num_edge;
}
inline void Dfs_For_Tree_Down(int pos,int fth)
{
for(int i=head[pos];i;i=edge[i].next)
if(edge[i].to!=fth&&!Vis[edge[i].to])
Dfs_For_Tree_Down(edge[i].to,pos),Son[pos]++;
for(int i=head[pos];i;i=edge[i].next)
if(edge[i].to!=fth&&!Vis[edge[i].to])
Down[pos]+=Down[edge[i].to]+edge[i].dis*1.0;
if(Son[pos]) Down[pos]=Down[pos]/(Son[pos]*1.0);
}
inline void Dfs_For_Tree_Up(int pos,int fth,double dis)
{
if(m==n-1)
{
if(Son[fth]&&fth!=1)
Up[pos]=(Up[fth]+Down[fth]*Son[fth]-Down[pos]-dis)/Son[fth]+dis;
else if(fth==1&&Son[fth]>1)
Up[pos]=(Up[fth]+Down[fth]*Son[fth]-Down[pos]-dis)/(Son[fth]-1)+dis;
else if(fth==1&&Son[fth]==1) Up[pos]=dis;
if(pos!=1) ans[pos]=(Down[pos]*Son[pos]+Up[pos])/(Son[pos]+1);
else ans[pos]=Down[pos];
}
else
{
if(Son[fth]&&!Vis[fth])
Up[pos]=(Up[fth]+Down[fth]*Son[fth]-Down[pos]-dis)/Son[fth]+dis;
else if(Vis[fth])
Up[pos]=(Up[fth]*2+Down[fth]*Son[fth]-Down[pos]-dis)/(Son[fth]+1)+dis;
ans[pos]=(Up[pos]+Down[pos]*Son[pos])/(1+Son[pos]);
}
for(int i=head[pos];i;i=edge[i].next)
if(edge[i].to!=fth&&!Vis[edge[i].to])
Dfs_For_Tree_Up(edge[i].to,pos,edge[i].dis*1.0);
}
inline void Find_Dia(int pos,int fth,int lid)
{
Dfn[pos]=++Tim;fa[pos]=fth,Id[pos]=lid;
for(int i=head[pos];i&&!Cnt;i=edge[i].next)
if(edge[i].to!=fth)
{
if(Dfn[edge[i].to])
{
if(Dfn[edge[i].to]>Dfn[pos])
{
for(int Now=edge[i].to;Now!=pos;Now=fa[Now])
{
Cir[++Cnt]=make_pair(Now,edge[Id[Now]].dis);
Vis[Now]=1,Tag[Now]=Cnt;
}
Cir[++Cnt]=make_pair(pos,edge[i].dis);
Vis[pos]=1,Tag[pos]=Cnt;
}
}else Find_Dia(edge[i].to,pos,i);
}
}
inline void Work(int Root)
{
Find_Dia(Root,0,0);
for(int i=1;i<=Cnt;i++) Dfs_For_Tree_Down(Cir[i].first,0);
for(int i=1;i<=Cnt;i++)
{
if(i!=1) Dis[i-1][i]=Dis[i][i-1]=Cir[i-1].second*1.0;
if(i!=Cnt) Dis[i][i+1]=Dis[i+1][i]=Cir[i].second*1.0;
}
Dis[1][Cnt]=Dis[Cnt][1]=Cir[Cnt].second*1.0;
for(int i=1;i<=Cnt;i++)
{
int Now=Cir[i].first;double P=1.0;
for(int j=i+1;j!=i;j++)
{
if(j>Cnt) j-=Cnt;if(j==i) break;
int Pos=Cir[j].first;int w=Dis[j][!(j-1)?Cnt:j-1];
if(j==(!(i-1)?Cnt:i-1)) Up[Now]+=P*(w+Down[Pos]);
else Up[Now]+=P*(w*1.0+Down[Pos]*Son[Pos]/(Son[Pos]+1)*1.0);
P/=Son[Pos]+1;
}
P=1.0;
for(int j=i-1;j!=i;j--)
{
if(j<=0) j+=Cnt;if(j==i) break;
int Pos=Cir[j].first;int w=Dis[j][(j+1)>Cnt?1:j+1];
if(j==((i+1)>Cnt?1:i+1)) Up[Now]+=P*(w+Down[Pos]);
else Up[Now]+=P*(w*1.0+Down[Pos]*Son[Pos]/(Son[Pos]+1)*1.0);
P/=Son[Pos]+1;
}
Up[Now]/=2;
}
for(int i=1;i<=Cnt;i++)
for(int j=head[Cir[i].first];j;j=edge[j].next)
if(!Vis[edge[j].to])
Dfs_For_Tree_Up(edge[j].to,Cir[i].first,edge[j].dis);
for(int i=1;i<=Cnt;i++)
{
int pos=Cir[i].first;
ans[pos]=(Up[pos]*2+Down[pos]*Son[pos])/(2+Son[pos]);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("A.in","r",stdin);
#endif
n=read(),m=read();
for(int i=1,u,v,d;i<=m;i++)
u=read(),v=read(),d=read(),Add(u,v,d),Add(v,u,d);
if(m==n) Work(1);
else Dfs_For_Tree_Down(1,0),Dfs_For_Tree_Up(1,0,0);
for(int i=1;i<=n;i++) Ans+=ans[i];printf("%.5lf",Ans/n*1.0);
}
标签:Up,NOI2012,游乐园,int,pos,Son,Down,迷失,fth 来源: https://www.cnblogs.com/wo-shi-zhen-de-cai/p/11614823.html