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

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

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

#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=;
const double INF=<<;
struct node
{
int total;
int weight;
vector<int>child;
};
node list[maxn];
queue<int >q;
int path[maxn];
int n,m,s;
bool cmp(int a,int b)
{
return list[a].weight>list[b].weight;
}
void dfs(int v,int num)
{
if(list[v].total>s)return;
if(list[v].total==s)
{
if(list[v].child.size()!=)return ;
for(int j=;j<num;j++)
{
if(j==num-)printf("%d\n",list[path[j]].weight);
else printf("%d ",list[path[j]].weight);
}
}
for(int i=;i<list[v].child.size();i++)
{
int t=list[v].child[i];
list[t].total+=list[v].total;
path[num]=t;
dfs(t,num+);
}
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k,id,tmp;
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%d",&list[i].weight);
list[i].total=list[i].weight;
}
for(i=;i<m;i++)
{
scanf("%d%d",&id,&k);
for(j=;j<k;j++)
{
scanf("%d",&tmp);
list[id].child.push_back(tmp);
}
sort(list[id].child.begin(),list[id].child.end(),cmp);
}
dfs(,);
}
return ;
}

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. ScalaTour 2.函数

    /** * 1. case class与模式匹配 */ object TestFunction extends App{ def value(expr:Expr): Int = expr match ...

  2. PLSQL_闪回操作5_Flashback Table

    2014-12-09 Created By BaoXinjian

  3. OAF_EO系列4 - Create详解和实现(案例)

    2014-06-02 Created By BaoXinjian

  4. SecureCRT 的安装和注册

    基本信息:win764位 准备: 下载软件包:scrt70-x86.exe,和SecureCRT.7.3.keygen.exe注册机 下载地址:http://download.csdn.net/det ...

  5. CentOS安装apache2(转载)

    From:http://www.onepx.com/centos-apache-246.html 之前服务器 Apache 版本一直是 2.2.x,鉴于 Centos 更新软件的惰性,我看直到 201 ...

  6. mysql 按年度、季度、月度、周、日SQL统计查询

    创建Table CREATE TABLE `test` ( `cdate` datetime DEFAULT NULL, `id` ) DEFAULT NULL, `name` ) DEFAULT N ...

  7. 20145305 《Java程序设计》第2周学习总结

    教材学习内容总结 1.boolean类型可表示true与false 2.储存值超类型范围会出现溢值 3.变量在命名时不可以使用数字作为开头,不可以使用一些特殊字符,不可以与JAVA关键词同名,不可以与 ...

  8. [Java] 使用转换流来处理标准输入

    package test.stream; import java.io.BufferedReader; import java.io.IOException; import java.io.Input ...

  9. I2C协议(转)

    1.I2C协议   2条双向串行线,一条数据线SDA,一条时钟线SCL.   SDA传输数据是大端传输,每次传输8bit,即一字节.   支持多主控(multimastering),任何时间点只能有一 ...

  10. 错误 1 Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。

    错误 1 Files 的值“ < < < < < < < .mine”无效.路径中具有非法字符.     今天使用SVN进行更新的时候,出现了如上问题,想起卓 ...