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 ...
随机推荐
- Unix commands in Mac OS X
参考:http://www.renfei.org/blog/mac-os-x-terminal-101.html One command line includes 4 parts: Command ...
- 基于redis的分布式锁
<?php /** * 基于redis的分布式锁 * * 参考开源代码: * http://nleach.com/post/31299575840/redis-mutex-in-php * * ...
- 为什么引用不了App_Code里的类
在Web应用程序中不能通过右键项目-〉”添加“-〉”添加ASP.NET文件夹“方式添加 .因为Web应用程序中App_Code就不存在 .不过可以通过手动的方式创建,添加一个文件夹命名为App_Cod ...
- 一款监控网络状态的好工具- Smokeping
最近工作中需要监测某个分公司到IDC机房的网络情况,到网络上找了不少软件,发现一款叫smokeping的开源软件还不错,它是rrdtool的作者制作的,在图形显示方面很漂亮,可以用来很好的检测网络状态 ...
- Ubuntu设置环境变量并立即生效
Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效. 修改用户环境变量 用户环境变量通常被存储在下面的文件中: ...
- CentOS7 备忘录
//安装CentOS7 1.用LinuxLive USB Creator,LiveUSB Creator设置U盘安装不成功,UltraISO可以. 2.旧电脑(x8le)安装时报"/dev/ ...
- esriSRGeoCS3Type Constants
ArcGIS Developer Help (Geometry) esriSRGeoCS3Type Constants More available geographic coordinat ...
- ZOJ 3407 Doraemon's Cake Machine [数学]
题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切 经过蛋糕圆心,将蛋糕整个向下切. 2.横切 平行于蛋糕平 ...
- 列出当前ARM开发板系统加载的模块
lsmod 列出当前系统中加载的模块,其中左边第一列是模块名,第二列是该模块大小,第三列则是使用该模块的对象数目
- oracle 表空间管理
1.创建表空间 03:01:55 sys@ORADB11G> create tablespace TSPITR datafile '/u01/app/oracle/oradata/TSPITR0 ...