//之前一直尝试用vector存储path,但是每次错误后回退上一级节点时不能争取回退,导致探索路径正确,但是输出不正确,用参数num,标记前一个路径点的位置传递参数,就好多了

//其中在输入时就将后继节点按照权值大小排列,是可以学习方法,再输入时就处理好,方便dfs探索,有几道PAT题目都是在该基础上对路径加要求,都可以直接改进输出那一部分代码,更新更和要求的路径,全部遍历完,就可得到所求符合要求的的路径

//参数传递的思想也不错,之前不习惯,总是直接把ans存进vector,最后输出

  1. #include<cstdio>
  2. #include<queue>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=;
  7. const double INF=<<;
  8. struct node
  9. {
  10. int total;
  11. int weight;
  12. vector<int>child;
  13. };
  14. node list[maxn];
  15. queue<int >q;
  16. int path[maxn];
  17. int n,m,s;
  18. bool cmp(int a,int b)
  19. {
  20. return list[a].weight>list[b].weight;
  21. }
  22. void dfs(int v,int num)
  23. {
  24. if(list[v].total>s)return;
  25. if(list[v].total==s)
  26. {
  27. if(list[v].child.size()!=)return ;
  28. for(int j=;j<num;j++)
  29. {
  30. if(j==num-)printf("%d\n",list[path[j]].weight);
  31. else printf("%d ",list[path[j]].weight);
  32. }
  33. }
  34. for(int i=;i<list[v].child.size();i++)
  35. {
  36. int t=list[v].child[i];
  37. list[t].total+=list[v].total;
  38. path[num]=t;
  39. dfs(t,num+);
  40. }
  41. }
  42. int main()
  43. {
  44. freopen("input.txt","r",stdin);
  45. int i,j,k,id,tmp;
  46. while(scanf("%d%d%d",&n,&m,&s)!=EOF)
  47. {
  48. for(i=;i<n;i++)
  49. {
  50. scanf("%d",&list[i].weight);
  51. list[i].total=list[i].weight;
  52. }
  53. for(i=;i<m;i++)
  54. {
  55. scanf("%d%d",&id,&k);
  56. for(j=;j<k;j++)
  57. {
  58. scanf("%d",&tmp);
  59. list[id].child.push_back(tmp);
  60. }
  61. sort(list[id].child.begin(),list[id].child.end(),cmp);
  62. }
  63. dfs(,);
  64. }
  65. return ;
  66. }

PAT1053. Path of Equal Weight的更多相关文章

  1. pat1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  2. Path of Equal Weight (DFS)

    Path of Equal Weight (DFS)   Given a non-empty tree with root R, and with weight Wi assigned to each ...

  3. 【PAT】1053 Path of Equal Weight(30 分)

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  4. PAT 1053 Path of Equal Weight[比较]

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  5. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  6. PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

  7. PAT_A1053#Path of Equal Weight

    Source: PAT A1053 Path of Equal Weight (30 分) Description: Given a non-empty tree with root R, and w ...

  8. 1053 Path of Equal Weight——PAT甲级真题

    1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...

  9. 1053. Path of Equal Weight (30)

    Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of ...

随机推荐

  1. c++ std::sort函数调用经常出现的invalidate operator<错误原因以及解决方法

    在c++编程中使用sort函数,自定义一个数据结构并进行排序时新手经常会碰到这种错误. 这是为什么呢?原因在于什么?如何解决? 看下面一个例子: int main(int, char*[]) { st ...

  2. Spark on Yarn 架构解析

    . 一.Hadoop Yarn组件介绍: 我们都知道yarn重构根本的思想,是将原有的JobTracker的两个主要功能资源管理器 和 任务调度监控 分离成单独的组件.新的架构使用全局管理所有应用程序 ...

  3. oracle 运维基础

    setupDatabase() { runStr=" cd $BASE_INSTALL_DIR/database nohup ./runInstaller -silent -force -r ...

  4. JAVA元运算符,一元运算符,二元运算符,三元运算符

    一元运算符: 序号 一元运算符 说明 1 i++ 给i加1 2 i-- 给i减1 3 ++i 给i加1 4 --i 给i减1 i++;/*例:int i=1;i++;//先将i的值1赋值给i,然后i再 ...

  5. java的内省(introspector)

    package com.wzh.test.introspector; import java.beans.BeanInfo; import java.beans.IntrospectionExcept ...

  6. win10安装软件被阻止后

    以管理员身份运行CMD,然后在cmd里执行就可以了.

  7. oninput 属性

    在HTML5中,新增加了oninput属性,它和onchange 的不同就是立刻发生,而onchange 是在失去焦点的时候才发生 <script> function rangeChang ...

  8. java中json包的使用以及字符串,map,list,自定义对象之间的相互转换

    做一个map和字符串的转换,需要导入这些jar包,这是最基本的一些jar包. 经过多方尝试得出结论入下: 首先导入基本包:json-lib-2.2.3-jdk15.jar 如果没有这个jar包,程序是 ...

  9. Delphi 线程的处理

    http://www.cnblogs.com/doit8791/archive/2012/05/16/2502671.html

  10. 使用spark访问elasticsearch的数据

    使用spark访问elasticsearch的数据,前提是spark能访问hive,hive能访问es http://blog.csdn.net/ggz631047367/article/detail ...