PAT1053. Path of Equal Weight
//之前一直尝试用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的更多相关文章
- pat1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
- 【PAT】1053 Path of Equal Weight(30 分)
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
- PAT 1053 Path of Equal Weight[比较]
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 Wi assigne ...
- 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 ...
- 1053 Path of Equal Weight——PAT甲级真题
1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...
- 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 ...
随机推荐
- stdlib
system(pause): int cmp(const void *a ,const void *b) { return *(int *)a - *(int *)b ; //从小到 ...
- JAVA 主函数(主方法)
主函数(主方法) 1.public (访问修饰符,公共的)代表该类或者该方法访问权限是最大的 2.static 代表主函数随着类的加载而加载 3.void 代表主函数没有具体的返回 ...
- 第十章 Vim程序编辑器学习
1.Vim是进阶版的vi,vim不但可以用不同颜色显示文字内容,还能进行诸如shell script,C program等程序编辑功能. 区别:vi是老师的字处理器,不过功能已经很齐全,但还是有可以进 ...
- JAVA中关于同步与死锁的问题
java中当多个现成同时操纵同一资源的时候需要考虑同步的问题.如车站售票,不同售票点卖同一班次车票的时候就要同步,否则卖票会有问题.下面代码模拟车站卖票: class TicketSeller imp ...
- Javascript同源策略对context.getImageData的影响
在本机测试HTML5 Canvas程序的时候,如果用context.drawImage()后再用context.getImageData()获取图片像素数据的时候会抛出错:SECURITY_ERR: ...
- ckfinder的配置使用
1.单纯的上传图片和预览图片 修改configasp中CheckAuthentication = true; 否则的话会报没有权限或修改配置错误 此时如果可以查看的话,单击图片应该是放大并且预览图片 ...
- 查看maven项目的依赖关系 mvn dependency:tree
maven-dependency-plugin最大的用途是帮助分析项目依赖,dependency:list能够列出项目最终解析到的依赖列表,dependency:tree能进一步的描绘项目依赖树,de ...
- nyoj 97 兄弟郊游问题
点击打开链接 兄弟郊游问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 兄弟俩骑车郊游,弟弟先出发,每分钟X米,M分钟后,哥哥带一条狗出发.以每分钟Y米的速度去追弟弟 ...
- 蓝桥杯---地宫取宝(记忆搜索=搜索+dp)
题目网址:http://lx.lanqiao.org/problem.page?gpid=T120 问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值 ...
- 硬盘安装ubuntu120.04分区方案
320G分区方案1/boot :256MB ext2swap :4G/ :40G ext4/tmp :5G ext4/var :20G ext4/usr :20G ext4/home :230GMB ...