PAT甲级1079 Total Sales of Supply Chain (25分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

Input Specification:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3

Sample Output:

42.4

二、解题思路

我们可以将输入的每个整体设为一个结构体,包括卖的总货量(如果是retailer),当前的单价以及供应链(如果是supplier)。随后用dfs更新每个retailer或者supplier的价格。因为最开始的supplier是0号,所以我们用dfs(0)即可。

三、AC代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 100010;
int N;
double pr, r, total = 0.0;
struct Supply
{
    
    
  int sze = 0;
  double price;
  vector<int> chain;
}sup[maxn];
void dfs(int st)
{
    
    
  if(sup[st].sze > 0)
  {
    
    
    total += sup[st].sze * sup[st].price;
    return;
  }
  for(int i=0; i<sup[st].chain.size(); i++)
  {
    
    
    sup[sup[st].chain[i]].price = r * sup[st].price;
    dfs(sup[st].chain[i]);
  }
  return;
}
int main()
{
    
    
  int num, tmp;
  scanf("%d%lf%lf", &N, &pr, &r);
  r = 1 + r/100;
  sup[0].price = pr;
  for(int i=0; i<N; i++)
  {
    
    
    scanf("%d", &num);
    if(num == 0)
      scanf("%d", &sup[i].sze);
    else
    {
    
    
      for(int j=0; j<num; j++)
      {
    
    
        scanf("%d", &tmp);
        sup[i].chain.push_back(tmp);
      }
    }
  }
  dfs(0);
  printf("%.1f", total);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/108746779