1090. Highest Price in Supply Chain

来源:互联网 发布:赌博软件破解 编辑:程序博客网 时间:2024/06/06 09:59

题目

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 price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be -1. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

基本思路

考察树的遍历。
1.采用静态写法。在前面做到过的关于二叉树的题目中,采用静态写法时结点可定义为:

struct node{    typename data;    int lchild,rchild;}Node[maxn];

普通的数与二叉树的区别在于,每个结点的孩子可能不止两个,即结点的指针域的个数是不确定,从而推广可将普通树的结点定义为:

struct node{    typename data;    vector<int> child;}Node[maxn];

2.本题要求最高的价格以及获得该价格的叶结点个数。用BFS和DFS均可实现。先讨论BFS版,在结点结构中定义结点的层次layer,问题即要我们求结点的最大深度。

BFS代码
#include<cstdio>#include<vector>#include<queue>using namespace std;const int maxn = 100010;struct node{    int layer;    vector<int> child;//指针域 }Node[maxn]; int n;double p,r;//结点数,原价,增长率 //计算第 k 层的价格 double price(int k){    for(int i=0;i<k;i++){        p *= (1 + r/100);    }    return p;} //通过层序遍历(BFS)进行遍历 int maxLayer = 0,num = 0;//最深层次 ,最深层次的叶结点个数 void levelOrder(int root){    queue<int> q;    q.push(root);    Node[root].layer = 0;//定义根结点为第0层    while(!q.empty()){        int top = q.front();        q.pop();        if(Node[top].layer > maxLayer){            maxLayer = Node[top].layer;            num = 1;//重置        }else if(Node[top].layer == maxLayer){            num++;        }        for(int i=0;i<Node[top].child.size();i++){//访问当前点的孩子结点             int kid = Node[top].child[i];            q.push(kid);             Node[kid].layer = Node[top].layer + 1;        }    }     return;}int main(){    scanf("%d%lf%lf",&n,&p,&r);    int father,root;    for(int i=0;i<n;i++){        scanf("%d",&father);        if(father != -1){            Node[father].child.push_back(i);//i 是father的孩子(读题)        }else{            root = i;        }    }    levelOrder(root);    printf("%.2f %d\n",price(maxLayer),num);     return 0;} 

这里有一个细节点需要注意,在第一遍做的时候,我把”最深层次”以及“最深层次的叶结点个数”的初值定义为 int maxLayer = 0,num = 1; ,这样提交会有一个1分的测试点无法通过,原因在于边界情况通不过,比如只有一个结点时,当样例输入为1 1.80 1.00 时,输出的结果是1.80 2 ,这显然是错了。再回头看levelOrder(int root)函数,会发现把初值定义成maxLayer = 0,num = 0;maxLayer = -1,num = 1; 就可以了。
3.用DFS实现时关键在于如何确定递归式和递归边界。本题中,递归边界就是到达叶结点,递归式就是对每个结点的孩子进行DFS。

DFS代码
#include<cstdio>#include<vector>using namespace std;const int maxn = 100010;struct node{    vector<int> child;//指针域 }Node[maxn]; int n;double p,r;//结点数,原价,增长率 //计算第 k 层的价格 double price(int k){    for(int i=0;i<k;i++){        p *= (1 + r/100);    }    return p;} //DFS实现,index为当前结点的下标,depth为当前结点的深度 int maxDepth = 0,num = 0;void DFS(int index,int depth){    if(Node[index].child.size() == 0){//到达叶结点         if(depth > maxDepth){            maxDepth = depth;            num = 1;//重置         }else if(depth == maxDepth){            num++;        }        return;     }    for(int i=0;i<Node[index].child.size();i++){        int kid = Node[index].child[i];        DFS(kid,depth+1);    }} int main(){    scanf("%d%lf%lf",&n,&p,&r);    int father,root;    for(int i=0;i<n;i++){        scanf("%d",&father);        if(father != -1){            Node[father].child.push_back(i);        }else{            root = i;        }    }    DFS(root,0);//DFS入口     printf("%.2f %d\n",price(maxDepth),num);     return 0;} 
0 0
原创粉丝点击