1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
   | #include<iostream> #include<cstdio> #include<cmath> using  namespace std; const int N=2e5+10; const double INF=1e30; template<class T> inline void read(T &x) {     x=0;int f=0;     char ch=getchar();     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}     while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();     if(f)x=~x+1; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) {     read(x),read(x1...); } int n,anspos,rt; int v[N],nowtot; int tot,head[N],ver[N<<1],e[N<<1],ne[N<<1]; int si[N],maxp[N]; bool vis[N]; double ans=INF,sum,dv[N];
  inline void add(int u,int v,int w) {     ver[++tot]=v;     e[tot]=w;     ne[tot]=head[u];     head[u]=tot; } void get_rt(int x,int fa) {     if(vis[x])return ;     si[x]=1;          maxp[x]=0;     for(int i=head[x];i;i=ne[i])     {         int y=ver[i];         if(y==fa||vis[y])continue;         get_rt(y,x);         si[x]+=si[y];         maxp[x]=max(maxp[x],si[y]);     }          maxp[x]=max(maxp[x],nowtot-si[x]);     if(maxp[x]<maxp[rt])rt=x; } void calc(int x,int fa,double &dv,int nowd) {     sum+=pow(nowd,1.5)*v[x],dv+=1.5*sqrt(nowd)*v[x];     for(int i=head[x];i;i=ne[i])     {         int y=ver[i];         if(y==fa)continue;         calc(y,x,dv,nowd+e[i]);     } } void dfs(int x) {     if(vis[x])return ;     rt=0;     get_rt(x,0);     x=rt;     vis[x]=1;     sum=0;     double sumd=0;     for(int i=head[x];i;i=ne[i])     {         int y=ver[i];         dv[y]=0;         calc(y,x,dv[y],e[i]);         sumd+=dv[y];     }     if(sum<ans)anspos=x,ans=sum;     for(int i=head[x];i;i=ne[i])     {         int y=ver[i];         if(sumd-2*dv[y]<=0)         {             nowtot=si[y];             dfs(y);             continue;         }     } } int main() {     read(n);     for(int i=1;i<=n;i++)read(v[i]);     for(int i=1;i<n;i++)     {         int u,v,w;         read(u,v,w);         add(u,v,w);         add(v,u,w);     }     nowtot=n;     maxp[0]=n;     dfs(1);     printf("%d %lf",anspos,ans);     return 0; }
   |