Luogu P2986 [USACO10MAR] great cows gather Great Cow Gat ... explanations

Cackle

This question is when I do the ultimate cerebral palsy. .

First submission forgot to open \ (Long \ Long \) , only \ (50 \) points;

The second output forget to submit open \ (Long \ Long \) , only \ (70 \) points. . .

Extremely silent. .

Face questions

Face questions

Solution

To find a point, such that \ (\ sum \ limits_ {j ! = Rt} dis_j \ cdot c_j \) minimum.

Consider changing root.

First find to (1 \) \ answer when the number of points for the root node, while the number of dairy cows recorded sub-tree inside. For each edge, its contribution to the \ (Val \ CDOT size_} to {\) .

Then \ (DFS \) to change the root. Go to consider the root \ (u \) , only \ (rt \) is connected to the \ (u \) changed sides will answer. specific:

  1. For to \ (u \) cow is the root of the subtree contained, they go away to reduce the \ (Val \) , the answer to the contribution of minus \ (size_u \ cdot Val \) .
  2. For other cows, they go away increased \ (Val \) , the answer contribution to add \ ((SIZE_1-size_u) \ cdot Val \) . (Because \ (size_1 \) represents the total number of cows)

Then the final answer is \ (\ min \ limits_. 1} = {I n-ans_i ^ \) .

Code

#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
    int f=1;char k=getchar();x=0;
    for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
    for(;k>='0'&&k<='9';k=getchar()) x=(x<<3)+(x<<1)+k-'0';
    x*=f;
}
template<class T>il print(T x){
    if(x/10) print(x/10);
    putchar(x%10+'0');
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int m,int mod){
    int res=1,bas=x%mod;
    while(m){
        if(m&1) res=(res*bas)%mod;
        bas=(bas*bas)%mod,m>>=1;
    }
    return res%mod;
}
const int MAXN = 1e5+5;
int n,c[MAXN],u,v,d,head[MAXN],num_edge,size[MAXN];
ll ans,now;
struct Edge{
    int next,to,dis;
    Edge(){}
    Edge(int next,int to,int dis):next(next),to(to),dis(dis){}
}edge[MAXN<<1];
il add_edge(int u,int v,int dis){
    edge[++num_edge]=Edge(head[u],v,dis),head[u]=num_edge;
    edge[++num_edge]=Edge(head[v],u,dis),head[v]=num_edge;
}
il DFS(int u,int fa){
    size[u]=c[u];
    for(ri i=head[u];i;i=edge[i].next){
        if(edge[i].to==fa) continue;
        DFS(edge[i].to,u),size[u]+=size[edge[i].to];
        ans+=1ll*size[edge[i].to]*edge[i].dis;
    }
}
il DFS1(int u,int fa,ll res){
    ans=min(ans,res);
    for(ri i=head[u];i;i=edge[i].next){
        if(edge[i].to==fa) continue;
        DFS1(edge[i].to,u,res-1ll*edge[i].dis*size[edge[i].to]+1ll*edge[i].dis*(size[1]-size[edge[i].to]));
    }
}
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    read(n);
    for(ri i=1;i<=n;++i) read(c[i]);
    for(ri i=1;i<n;++i) read(u),read(v),read(d),add_edge(u,v,d);
    DFS(1,0),now=ans;
    for(ri i=head[1];i;i=edge[i].next) DFS1(edge[i].to,1,now-1ll*edge[i].dis*size[edge[i].to]+1ll*edge[i].dis*(size[1]-size[edge[i].to]));
    printf("%lld",ans);
    return 0;
}

to sum up

Do question must be considered a good range of data, determine good use \ (int \) or use \ (Long \ Long \) , or else blown up on the bad.

Guess you like

Origin www.cnblogs.com/TheShadow/p/11390947.html