A1079 Total Sales of Supply Chain (25分)

一、技术总结

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

二、参考代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct node{
    double data;
    vector<int> child;
}Node[maxn];
int n;
double p, r, sum = 0;//用于记录总费用
void DFS(int index, int depth){
    if(Node[index].child.size() == 0){
        sum += 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(){
    scanf("%d%lf%lf", &n, &p, &r);
    r /= 100;
    int num, id;//num用于记录每一个结点的子结点数量, id子结点编号
    for(int i = 0; i < n; i++){
        scanf("%d", &num);
        if(num == 0){
            scanf("%lf", &Node[i].data);
        }else{
            for(int j = 0; j < num; j++){
                scanf("%d", &id);
                Node[i].child.push_back(id);
            }
        }
    }
    DFS(0, 0);
    printf("%.1f\n", p*sum);
    return 0;
}

原文地址:https://www.cnblogs.com/tsruixi/p/12321855.html

时间: 2024-10-05 00:11:22

A1079 Total Sales of Supply Chain (25分)的相关文章

树的遍历——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

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

【PAT甲级】1079 Total Sales of Supply Chain (25 分)

题意: 输入一个正整数N(<=1e5),表示共有N个结点,接着输入两个浮点数分别表示商品的进货价和每经过一层会增加的价格百分比.接着输入N行每行包括一个非负整数X,如果X为0则表明该结点为叶子结点接着输入一个整数表示该零售商进货的数量,X不为零则接着输入X个正整数表示它的下级经销商是哪些结点.输出所有零售商进货的总价.(结点从0~N-1,0为根节点即供应商) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using name

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 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

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

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

PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)

树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std

PAT:1079. Total Sales of Supply Chain (25) AC

#include<stdio.h> #include<math.h> #include<vector> using namespace std; const int MAX=100010; int DEPest=0; int root=-1,N; double P,r,sum=0; vector<int> child[MAX]; //child[父亲][孩子] 二维数组表示树形结构 double price[MAX]; //记录叶子节点的权值 void DF