PAT A1079 Total Sales of Supply Chain(关于树的遍历)

DFS遍历

#include<cstdio>

#include<cmath>

using name spacestd;

const int maxn=10012;

struct node{

double data;

vector<int> child;

}Node[maxn];

int n;

double p,r,ans=0;

void DFS(int index,int depth){

if(Node[index].child.size()==0){

ans +=Node[index].data*pow(1+r,depth);

return;

}

for (int i=0;i<Node[index].child.size();i++){

DFS(Node[index].child[i],depth+1);

}}

int main(){

int x,child;

scanf("%d%lf%lf",&n,&p,&r);

r /=100;

for(int i=0;i<n;i++){

scanf("%d",&k);

if(k==0){

scanf("%lf",&Node[i].data);

}else{

for (int j=0;j<k;j++){

scanf("%d",&child);

Node[i].child.push_back(child);

}}}

DFS(0,0);

printf("%.lf‘\n",p*ans);

return 0;

}

原文地址:https://www.cnblogs.com/jicheng1996/p/9691831.html

时间: 2024-10-03 16:56:43

PAT A1079 Total Sales of Supply Chain(关于树的遍历)的相关文章

树的遍历——A1079.Total Sales of Supply Chain(25) 与A1090类似

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 100010; struct node{ double data; vector<int> child; }Node[maxn]; int n; double p,r,ans = 0; void DFS(int

PAT 1079. Total Sales of Supply Chain (25)

1079. Total Sales of Supply Chain (25) A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys

PAT 1079 Total Sales of Supply Chain[比较]

1079 Total Sales of Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys

A1079. Total Sales of Supply Chain (25)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys products from one's supplier in a pric

A1079 Total Sales of Supply Chain (25分)

一.技术总结 开始拿到这一题,知道用DFS但是不知道怎么设置递归式和递归边界,一直在想,其实就是该节点的子结点为0时就是终止时,递归式就是每次递归后,对于深度depth加一. 还有一点就是怎么解决所有费用相加的问题,开始还在想,用一个数组存储所有路径然后再,在遍历,是在太笨了,可以直接定义一个全局变量sum,然后出现递归边界时,就加上这个路径下产生的总费用. 还有就是要注意题中给出的输出格式,还有就是英文中数字1和字母l过于相似,切记可能出现错误. 同时如果定义为double,用%d输入可能出现

pat1079. Total Sales of Supply Chain (25)

1079. Total Sales of Supply Chain (25) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to c

PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]

题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys products from one's supplier in a pr

PAT1079. Total Sales of Supply Chain

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys products from one's supplier in a pric

1079. Total Sales of Supply Chain (25)

时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root supplie