题意是给你一棵树    n个点 n-1条边   起点是1   每一个点都有权值 每次能从根节点走到叶子节点   经行k次游戏 每次都是从1開始    拿过的点的权值不能拿第二次   问最大权值和。

開始看到题时也没想到什么方法  就依照常规的来  结果超时了   试着优化了好多次  最后过了   百度题讲解是树链剖分    醉了    还没学!

。。

说说我的做法吧    map【i】=a表示i节点的跟节点为a节点   从全部叶子节点開始入队(有点队列里有三个变量  各自是节点编号  权值  深度  优先级看代码    里面有点贪心的意思) 每次走根节点   假设根节点没走过  则走它   并把该店权值变为0    否则直接跳到1这个节点(假设一个个跳可能会超时)再入队    当出队的编号为1时而且拿的个数小于游戏次数 则拿  否则结束  在跑深度的时候有个优化
    開始没有超时了            假设该节点深度已知了  则以后的根节点就不用跑了。!

详细看代码吧

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #include<iostream>
  5. using namespace std;
  6. int map[100010],mark[100010];
  7. int Deep[100010];
  8. __int64 num[100010];
  9. struct node
  10. {
  11. __int64 value;
  12. int ii;
  13. int deep;
  14. bool operator < (const node& x) const
  15. {
  16. return deep<x.deep||(deep==x.deep&&value<x.value);
  17. }
  18. }a;
  19. int main()
  20. {
  21. int T,i,j,n,k,r=1;
  22. scanf("%d",&T);
  23. while(T--)
  24. {
  25. scanf("%d%d",&n,&k);
  26. for(i=1;i<=n;i++)
  27. {
  28. scanf("%I64d",&num[i]);
  29. }
  30. memset(mark,0,sizeof(mark));
  31. for(i=1;i<n;i++)
  32. {
  33. int x,y;
  34. scanf("%d%d",&x,&y);
  35. mark[x]=1;
  36. map[y]=x;
  37. }
  38. priority_queue<node>Q;
  39. memset(Deep,0,sizeof(Deep));
  40. for(i=1;i<=n;i++)
  41. {
  42. if(mark[i]==0)
  43. {
  44. int x=map[i];
  45. int d=1;
  46. while(x!=1)
  47. {
  48. if(Deep[x]>0) {d+=Deep[x];break;}
  49. x=map[x];
  50. d++;
  51. }
  52. x=i;
  53. while(x!=1)
  54. {
  55. if(Deep[x]>0) break;
  56. Deep[x]=d;
  57. x=map[x];
  58. d--;
  59. }
  60. a.deep=Deep[i];
  61. a.value=num[i];
  62. a.ii=i;
  63. Q.push(a);
  64. }
  65. }
  66. //for(i=1;i<=n;i++)
  67. //printf("%d ^^^ %d\n",i,Deep[i]);
  68. __int64 sum=0;
  69. int cont=0;
  70. while(!Q.empty())
  71. {
  72. a=Q.top();
  73. Q.pop();
  74. int x=map[a.ii];
  75. /*while(num[x]==0&&x!=1)
  76. {
  77. x=map[x];
  78. }*/
  79. if(a.ii==1)
  80. {
  81. a.value+=num[1];
  82. num[1]=0;
  83. sum+=a.value;
  84. cont++;
  85. if(cont>=k) break;
  86. }
  87. else
  88. {
  89. if(num[x]==0)
  90. {
  91. a.ii=1;
  92. a.deep=0;
  93. }
  94. else
  95. {
  96. a.ii=x;
  97. a.deep=Deep[x];
  98. a.value+=num[x];
  99. num[x]=0;
  100. }
  101. Q.push(a);
  102. }
  103. }
  104. printf("Case #%d: %I64d\n",r++,sum);
  105. }
  106. return 0;
  107. }

hdu5242 上海邀请赛 优先队列+贪心的更多相关文章

  1. [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

    Description  Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...

  2. 最高的奖励 - 优先队列&贪心 / 并查集

    题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...

  3. POJ2431 优先队列+贪心 - biaobiao88

    以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...

  4. hdu3438 Buy and Resell(优先队列+贪心)

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)

    Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...

  6. ZOJ-3410Layton's Escape(优先队列+贪心)

    Layton's Escape Time Limit: 2 Seconds      Memory Limit: 65536 KB Professor Layton is a renowned arc ...

  7. CodeForces - 853A Planning (优先队列,贪心)

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  8. poj2431(优先队列+贪心)

    题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...

  9. H - Expedition 优先队列 贪心

    来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...

随机推荐

  1. mariadb的安装

    mysql (分支 mariadb)1.安装mariadb -yum -源码编译安装 -下载rpm安装 yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./co ...

  2. Laravel5.1学习笔记20 EloquentORM 关系

    Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has ...

  3. dropdownlist显示树形结构

    /// <summary> /// 递归 /// </summary> /// <param name="deplist"></param ...

  4. React容器组件和展示组件

    Presentational and Container Components   展示组件   - 只关心它们的样子.   - 可能同时包含子级容器组件和展示组件,一般含DOM标签和自定的样式.   ...

  5. Android项目实战_手机安全卫士splash界面

    - 根据代码的类型组织包结构 1. 界面 com.hb.mobilesafe.activities 2. 服务 com.hb.mobilesafe.services 3. 业务逻辑 com.hb.mo ...

  6. JS——滚动条

    1.核心思想与之前的拖拽盒子是一样的 2.完全将鼠标在盒子中的坐标给滚动条是错的,因为这样会使滚动条顶部立刻瞬间移动到鼠标位置 3.必须在鼠标按下事件时记住鼠标在滚动条内部的坐标,再将鼠标在盒子中的坐 ...

  7. JS——行内式注册事件

    html中行内调用function的时候,是通过window调用的function,所以打印this等于打印window,所以在使用行内注册事件时务必传入参数this <!DOCTYPE htm ...

  8. Codeforces_718A

    A. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. 关于在win7旗舰版32位上 安装 net4.0 的闪退问题研究 和安装sqlserver2008问题

    1.配置文件客户端[目标x86x64]的 可以安装 2.配置文件完全的目标x86x64的 出现闪退. 3.服务器核心的出现无法安装 安装 sqlserver 2008R2数据库 报错 \最后留下了它, ...

  10. day07补充-数据类型总结及拷贝

    目录 数据类型总结 按照存一个值 OR 多个值来分 按照有序 OR 无序来分 按照可变 OR 不可变来分 拷贝 && 浅拷贝 && 深拷贝&& .cop ...