1079. Total Sales of Supply Chain (25)

来源:互联网 发布:周立功单片机同类公司 编辑:程序博客网 时间:2024/06/07 03:45

题目详情:https://www.patest.cn/contests/pat-a-practise/1079

提交情况:这里写图片描述

代码:

#include <iostream>#include <vector>#include <math.h> using namespace std;#define Max 100010//depths[]存储各节点的深度 //isRoot[]存储各个节点是否为根节点,如果isRoot[i] != 0则代表节点i为叶子节点//depths[]存储各节点的深度 int n,visit[Max],isRoot[Max],depth,depths[Max];double rootPrice,percent;vector<int> child[Max];void DFS( int vertex,int depth )  //深度遍历,求各节点的深度 {    visit[vertex] = 1;    depths[vertex] = depth;    for( int i=0;i<child[vertex].size();++i )    {        int key = child[vertex][i];        if( visit[key] == 0 )        {            ++depth;            DFS(key,depth);            --depth;        }    }}int main (){    cin>>n>>rootPrice>>percent;    for( int i = 0;i<n;++i )    {        int num,key,count;        cin>>num;        if( num == 0 )  //代表节点i为叶子节点        {            cin>>count;            isRoot[i] = count;        }         //非叶子节点。叶子节点一开始就不满足循环条件         for( int j = 0;j < num;++j )        {            cin>>key;            child[i].push_back(key);        }           }    //DFS前的初始化    depth = 1;    DFS(0,depth);    double total = 0;    for( int i=0;i<n;++i )    {        if( isRoot[i] != 0 )  //代表为叶子节点         {            total += rootPrice*pow(1+percent/100,depths[i]-1)*isRoot[i];            //rootPrice*pow(1+percent/100,depths[i]-1)求出各个叶节点的价格            //isRoot[i]是各个叶节点中产品的数量        }    }    printf("%.1lf\n",total);    return 0;}

用vector作为树的存储结构,DFS()遍历求出各节点的深度,然后根据各个叶节点的价格乘以数量求出总的销售额。是不是说的太简单了?本题和 1090题类似。

0 0