标签:add cli def ali osi lock typedef cti script
2017.9.12新加数据一组 By GXZlegend
#include<ctime> #include<cstdio> #include<cstdlib> #define min(a,b) (a<b?a:b) #define max(a,b) (a>b?a:b) const int N=2000+5; const int M=N<<1; typedef long long ll; int tot,to[M],val[M],next[M],head[N],size[N];bool vis[N]; int n,K;ll f[N][N]; inline void add(int x,int y,int z){ to[++tot]=y;val[tot]=z;next[tot]=head[x];head[x]=tot; } void dp(int x){ int y,w,p,q; size[x]=1;vis[x]=1; for(int i=head[x];i;i=next[i]){ if(vis[y=to[i]]) continue; dp(y); w=val[i]; p=min(size[x],K); q=min(size[y],K);//常数优化 for(int j=p;~j;j--){ for(int k=q;~k;k--){ ll tv=1LL*(k*(K-k)+(n-size[y]-K+k)*(size[y]-k))*w; f[x][j+k]=max(f[x][j+k],f[x][j]+f[y][k]+tv); } } size[x]+=size[y];//常数优化 } } int main(){ srand(time(0));srand(rand()); scanf("%d%d",&n,&K);K=min(K,n-K); for(int i=1,x,y,z;i<n;i++) scanf("%d%d%d",&x,&y,&z),add(x,y,z),add(y,x,z); int root=rand()%n+1;//常数优化 dp(root); printf("%lld",f[root][K]); return 0; }
参考:
https://acxblog.site/archives/sol-bzoj-4033.html
https://blog.csdn.net/Diogenes_/article/details/81044483
https://www.cnblogs.com/leom10/p/11199602.html
标签:add cli def ali osi lock typedef cti script
原文地址:https://www.cnblogs.com/shenben/p/11603476.html