1090 Highest Price in 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 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
题目其实就是给定了个单向图,从根节点开始,找最深的层次。我用的广搜实现的,但是因为这里N也不小,10^5 ,所以存储的时候使用了Vector<vector< int >> vec(N) 每个结点一个vector< int > 来存储从该节点出去的边,不用数组是我感觉会超时,不如双vector嵌套来记录这个单向图
然后一个广搜搞定,然后有个测试点考察的就是根节点。
大概就是下面我的灵魂配图
一个结点有多个供应商(父节点,也无所谓,后面再次访问到了这个结点就更改下,因为在广搜里面,后面访问到的一定层次是更深的)
#include <iostream>
#include <string>
using namespace std;
#include<vector>
#include<algorithm>
#include<queue>
int sum(int n)//中序遍历
{
int out=0;
while(n!=0)
{
out+=n%10;
n=n/10;
}
return out;
}
int main()
{
int N;
double P;
double r;
cin>>N>>P>>r;
vector<vector<int>> vec(N);
int root;
for(int i=0; i<N; i++)
{
int num=i;//结点编号
int sup;
cin>>sup;//供应商
if(sup!=-1)
{
vec[sup].push_back(num);
}
else
root=num;
}
int level_num;
queue<int> que;
que.push(root);
int number=1;//有个测试点考察的就是最高价格就是根节点的价格,输出就是1
while(!que.empty())
{
queue<int> get;
while(!que.empty())
{
vector<int> number=vec[que.front()];
int start=que.front();
for(int i=0; i<number.size(); i++)
{
int endd=number[i];
get.push(number[i]);
}
que.pop();
}
if(get.size()!=0){ //这里是因为当进行到最后一个层次的时候,que的队列全部出栈,get为空
que=get;
number=get.size();
P=P*r*0.01+P;
}
}
printf("%.2f %d",P,number);
return 0;
}