1090. Highest Price in Supply Chain (25)
来源:互联网 发布:淘宝双皇冠店铺多少钱 编辑:程序博客网 时间:2024/06/03 18:30
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,DFS 2,BFS//法1:DFS#include<cstdio>#include<cmath>#include<vector>using namespace std;const int maxn=100010;struct Node{ int data; vector<int> child;}node[maxn];double p,r;int count[1000],maxD=-1;void DFS(int root,int depth){ if(node[root].child.size()==0){ count[depth]++; maxD=max(maxD,depth); } for(int i=0;i<node[root].child.size();i++){ DFS(node[root].child[i],depth+1); }}int main(){ int n; scanf("%d%lf%lf",&n,&p,&r); int root; for(int i=0;i<n;i++){ int addr; scanf("%d",&addr); if(addr==-1) root=i; else{ node[addr].child.push_back(i); } } DFS(root,0); double ans=0; printf("%.2lf %d\n",p*pow(1+r/100,maxD),count[maxD]); return 0; } //法2:BFS#include<cstdio>#include<cmath>#include<vector>#include<queue>using namespace std;const int maxn=100010;struct Node{ int data; int layer; vector<int> child;}node[maxn];double p,r;int maxL=-1,cnt=0;void BFS(int root){ queue<int> q; node[root].layer=0; q.push(root); while(!q.empty()){ int front=q.front(); q.pop(); for(int i=0;i<node[front].child.size();i++){ int child=node[front].child[i]; node[child].layer=node[front].layer+1; q.push(child); if(node[child].layer>maxL){ maxL=node[child].layer; cnt=1; }else{ cnt++; } } }}int main(){ int n; scanf("%d%lf%lf",&n,&p,&r); int root; for(int i=0;i<n;i++){ int addr; scanf("%d",&addr); if(addr==-1) root=i; else{ node[addr].child.push_back(i); } } BFS(root); printf("%.2lf %d\n",p*pow(1+r/100,maxL),cnt); return 0; }
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- String.spli()用法小结
- 我的threejs学习笔记(七)——spotLight
- BZOJ 1797 [Ahoi2009]Mincut 最小割
- TCP三次握手及其背后的缺陷
- 适配器模式在Android开发中的应用
- 1090. Highest Price in Supply Chain (25)
- 【最小割->最短路】BZOJ1001(BeiJing2006)[狼抓兔子]题解
- JVM、Dalvik VM和ART虚拟机之间的区别
- manacher算法
- L1-3情人节
- 整理Cocos2d-x 面试题解
- C++线程安全的单例模式
- OpeMP并行程序设计时需要注意的事项
- 多dex拆包原理