记录编号 60298 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [NOI 2011]道路修建 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 6.878 s
提交时间 2013-05-21 21:09:21 内存使用 24.25 MiB
显示代码纯文本
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #define F_I "noi2011_road.in"
  6. #define F_O "noi2011_road.out"
  7. using namespace std;
  8. typedef long long ll;
  9. const ll Nx=1000010;
  10. ll N,f[Nx],Ans,Pr[Nx],W[Nx];
  11. class Node{
  12. public:
  13. ll n,w;
  14. Node *pr;
  15. }*Adj[Nx];
  16. class Queue{
  17. public:
  18. ll h,t,li[Nx];
  19. void bfs(){
  20. h=t=1;
  21. li[t++]=1;
  22. while(h<t){
  23. ll H=li[h++];
  24. f[H]=1;
  25. for(Node *p=Adj[H];p;p=p->pr)
  26. if(f[p->n]==0){
  27. li[t++]=p->n;
  28. Pr[p->n]=H;
  29. W[p->n]=p->w;
  30. }
  31. }
  32. while(--t)
  33. f[Pr[li[t]]]+=f[li[t]];
  34. }
  35. }Q;
  36. void add(int u,int v,int w){
  37. Node *p=new Node;
  38. p->n=v,p->w=w;
  39. p->pr=Adj[u];
  40. Adj[u]=p;
  41. }
  42. void init(){
  43. ll i,u,v,w;
  44. scanf("%lld",&N);
  45. memset(Adj,0,sizeof(Adj));
  46. for(i=1;i<N;i++){
  47. scanf("%lld%lld%lld",&u,&v,&w);
  48. add(u,v,w);
  49. add(v,u,w);
  50. }
  51. }
  52. int main(){
  53. freopen(F_I,"r",stdin);
  54. freopen(F_O,"w",stdout);
  55. init();
  56. Q.bfs();
  57. for(int i=2;i<=N;i++)
  58. Ans+=W[i]*abs(N-f[i]-f[i]);
  59. cout<<Ans<<endl;
  60. return 0;
  61. }