PAT A 1090 Highest Price in Supply Chain 深搜

本文介绍了一种算法,用于计算供应链中产品的最高零售价格。该算法通过构建供应链成员之间的树形结构,找到最长的供应链路径,并据此计算最高价格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1090. Highest Price in Supply Chain (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

题意:大致题意就是说一个进货的图,卖个下一个人就提高进价%r,问你最后最大的价格是多少?

1 5 4 4 -1 4 5 3 6 这些数表示他的上一个进货的人,编号为0- n-1

思路1:这道题比较难得地方在于建立一个树,这个父节点为-1的结点一定是这个树的根节点,然后从根节点往下深搜找到树的深度最大的叶节点,就可以求出结果了;

我是第一次接触自己建立树的搜索,以前做的都是直接给你的图;建立树一般采用二维数组,我采用c++里vector容器来存树;

ac代码:

 #include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <algorithm>  
#include <iostream>  
#include <vector>  
using namespace std;
vector<int >a[100017];
int maxdep;
int cont;
int n;
void dfs(int tem,int dep)
{   if(a[tem].size()==0)
     {   if(dep>maxdep)
         {  cont=1;
            maxdep=dep;
}
else if(maxdep==dep)
 cont++;
}
for(int i=0;i<a[tem].size();i++)
 dfs(a[tem][i],dep+1);
}
int main()
{  double pri,r;
   while(scanf("%d %lf %lf",&n,&pri,&r)!=EOF)
   { 
   int x,i;
   int temp;
   maxdep=0;
   cont=0;
   for(i=0;i<n;i++)
   {  scanf("%d",&x);
      if(x==-1)
      {  temp=i;
         continue;
 }
 a[x].push_back(i);
   }
   dfs(temp,0);
   double ans=pri;
   for(i=0;i<maxdep;i++)
   {  ans=ans*(100+r)/100;
   }
    printf("%.2lf %d\n",ans,cont); 
}
return 0;
}

思路2:思路一我是通过根节点去找叶节点,这里我的第二种思路是通过叶节点去找到根节点,然后判断最大深度即可,因为从叶节点到根节点用一维数组就可以存啦。。。。。。但是超时了。。。。。还望哪位大神留言指教一下啊、、、、

   #include<stdio.h>
#include<string.h>
#include<math.h>
#define N 111111
int pre[N],deep[N];
int n;
double price ,r;
void dfs(int x)
{   if(pre[x]==-1)
    {   
    
   return ;
}
else 
{  
  dfs(pre[x]);
  deep[x]=deep[pre[x]]+1;
}
return ;
}
int main()
{   
    int i;
    scanf("%d %lf %lf",&n,&price,&r);
    memset(deep,0,sizeof(deep));
    for(i=0;i<n;i++)
    {   scanf("%d",&pre[i]);
}
for(i=0;i<n;i++)
{    if(deep[i]==0)
      dfs(i);
}
int count=0,max;
for(i=0;i<n;i++)
{    if(deep[i]>max)
       {
max=deep[i];
count=1;}
else if(deep[i]==max)
 count++;

for(i=0;i<max;i++)
   price*=1+r/100;
   printf("%.2lf %d",price,count);
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marcus-Bao

万水千山总是情,只给五角行不行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值