1090. Highest Price in Supply Chain (25)
来源:互联网 发布:dos复制文件到linux 编辑:程序博客网 时间:2024/05/15 22:48
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.001 5 4 4 -1 4 5 3 6Sample Output:
1.85 2
这道题相当于求供应链中最长的链长度为多少,求出最大长度就能求出最大的价格。用深度优先遍历一遍即可。
代码:
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>#include <cmath>using namespace std;int n;double p,r;vector<vector<int> >Next;int max_deep=0;int count=0;void dfs(int cur,int deep){if(Next[cur].size()==0) {if(max_deep<deep){max_deep=deep;count=1;}else if(max_deep==deep){count++;}return;}for(int i=0;i<Next[cur].size();i++){dfs(Next[cur][i],deep+1);}}int main(){cin>>n>>p>>r;Next.resize(n);int root;for(int i=0;i<n;i++){int supplier;cin>>supplier;if(supplier==-1) root=i;else Next[supplier].push_back(i);}dfs(root,0);double res=p*pow(1+r/100,max_deep);printf("%.2f %d",res,count);}
- 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)
- 树莓派3B安装QT5
- coderforces 670D2 Magic Powder - 2
- 进程通信之文件映射对象
- java web-1:走进java web
- python3 pymysql 'latin-1' codec can't encode character 错误 问题解决
- 1090. Highest Price in Supply Chain (25)
- 【NBUTOJ】Red packet
- Leetcode 377. Combination Sum IV
- POJ-3258 River Hopscotch
- input标签file 多文件上传
- Pie<poj3122>
- Google hacker工具使用和Google hacker防范
- Spring4学习笔记----Spring的依赖注入详解
- HTML5 Video标签的属性、方法和事件汇总介绍