比赛 20161115 评测结果 AWWWAAWWWWWWWWWWWWWW
题目名称 树和机器人 最终得分 15
用户昵称 残星誓言 运行时间 0.126 s
代码语言 C++ 内存使用 1.25 MiB
提交时间 2016-11-15 11:50:09
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const int maxn=50000+10;
  6. const int maxm=50000+10;
  7. int n,k,r;
  8. int ma=0;
  9. int head[maxn];
  10. bool vis[maxn];
  11. struct edges
  12. {
  13. int f;int t; int dist; int next;
  14. } e[maxm];
  15. int dfs(int x)
  16. {
  17. int re=0; vis[x]=1;
  18. for(int i=head[x];~i;i=e[i].next)
  19. {
  20. if(!vis[e[i].t])
  21. {
  22. int tp=dfs(e[i].t)+e[i].dist;
  23. if(x==r) ma=max(ma,tp);
  24. re+=tp;
  25. }
  26. }
  27. return re;
  28. }
  29. int tot=0;
  30. int ans=0;
  31. void ade(int f,int t,int d)
  32. {
  33. tot++;
  34. e[tot].f=f;
  35. e[tot].t=t;
  36. e[tot].dist=d;
  37. e[tot].next=head[f];
  38. head[f]=tot;
  39. }
  40. int main()
  41. {
  42. freopen("trobot.in","r",stdin);
  43. freopen("trobot.out","w",stdout);
  44. scanf("%d%d%d",&n,&r,&k);
  45. memset(vis,0,sizeof(vis));
  46. memset(head,-1,sizeof(head));
  47. for(int i=1;i<n;i++)
  48. {
  49. int a,b,c;
  50. scanf("%d%d%d",&a,&b,&c);
  51. ade(a,b,c);
  52. ade(b,a,c);
  53. ans+=c;
  54. }
  55. if(k==1)
  56. {
  57. int all=dfs(r);
  58. all=all*2-ma;
  59. printf("%d",all);
  60. }
  61. else
  62. {
  63. printf("%d",ans);
  64. }
  65. return 0;
  66. }