bzoj1296

来源:互联网 发布:大数据时代pdf百度云 编辑:程序博客网 时间:2024/06/05 12:41

啊啊啊啊啊,满脑子CO2,
竟然链式前向星数组没开两倍,调了10min RE
我太菜了
还是少喝碳酸饮料

#include<bits/stdc++.h>using namespace std;#define FOR(i,s,t) for(int i=(s);i<=(t);i++)inline int read(void){    int x = 0, c, f = 1;    do{c=getchar();if(c=='-')f=-1;}while(c<'0'||c>'9');    do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');    return x * f;}typedef long long LL;#define INF LONG_LONG_MAXconst int N = 110000;int a[N] ,n, to[2*N], fir[N], dis[2*N], nxt[2*N], Index, fa[N];LL C[N], fad[N];LL f[N], pre[N], pt[N], ans = INF, d;inline void addedge(int u,int v,int s){    to[++Index] = v, dis[Index] = s, nxt[Index] = fir[u], fir[u] = Index;}void dfs(int x, int Fa) {    C[x] = a[x];    for (int i = fir[x]; i; i = nxt[i]) if(to[i] != fa[x]){        if (to[i] == Fa)continue;         fa[to[i]] = x, fad[to[i]] = dis[i];        dfs(to[i], x);        C[x] += C[to[i]];        f[x] += C[to[i]] * dis[i] + f[to[i]];    }}void DP(int x, int Fa){    pre[x] = fa[x] == 0 ? 0 : pre[fa[x]] + C[fa[x]] - C[x];    pt[x] = fa[x] == 0 ? 0 : pt[fa[x]] + f[fa[x]] - C[x] * fad[x] - f[x] + pre[x] * fad[x];    ans = min(ans, pt[x] + f[x]);    for (int i = fir[x]; i; i = nxt[i]) if(to[i] != fa[x]) {        if (to[i] == Fa)continue;        DP(to[i], x);    }}int main(){    n = read();    for (int i = 1; i <= n; i++) a[i] = read();    for (int i = 1, u ,v, s; i < n; i++) {        u = read(), v = read(), s = read();        addedge(u,v,s), addedge(v,u,s);    }     dfs(1,0), DP(1,0);    printf("%lld\n",ans);}
0 0
原创粉丝点击