PAT甲级--Total Sales of Supply Chain (25)

这篇博客介绍了一个利用深度优先搜索(DFS)解决供应链中计算最高价格的问题。题目要求根据供应商、分销商和零售商之间的关系,以及增长率,计算最终的销售额。博主通过输入处理构建了树形结构,并用广度优先搜索(BFS)更新各节点的增长率,最后输出总销售额。代码清晰地展示了如何处理输入、计算和输出过程。
摘要由CSDN通过智能技术生成



与这题非常类似的题目,我用的dfs解的----- Highest Price in Supply Chain

题目


OJ平台

题目翻译

  • 关键句意:

where in the i-th line, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID’s of these distributors or retailers.
其中,在第i行中,Ki是从供应商i接收产品的分销商或零售商的总数,然后是这些分销商或零售商的ID。(注意i是它们的供应商!)

Kj being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after Kj.
Kj为0意味着第j个成员是零售商,那么产品的总数量将在Kj之后给出。其实就是为0就代表它没有继续分销,直接开始零售了,接下来给出的就是他所进货的多少了。

代码详解

输入处理

void Input() {
    ios::sync_with_stdio(false);
    cin >> N >> p >> r;
    for (int i = 0; i < N; i++) { /*建树*/
        int t; cin >> t;
        if (t)
            while (t--) {
                int n; cin >> n; //更新它的子节点
                node[i].emplace_back(n);
            }
        else { //记录结束的结点(零售商)以及它的购买数量
            int n ; cin >> n;
            mp.emplace_back(make_pair(i, n));
        }
    }
}

输出处理

先用bfs进行一次层序更新一下对应结点的增长率!再进行答案的更新。

void bfs() {
    Q.push(0); //利用bfs得出层级更新利率rr
    while (!Q.empty()) {
        int x = Q.front(); Q.pop();
        for (auto && t : node[x]) {
            rr[t] = rr[x] + rr[x] * (r / 100.0);
            Q.push(t);
        }
    }
}
void print() {
    bfs();
    double res = 0;
    for (auto && t : mp) {
        res += rr[t.first] * (double)t.second * p;
    }
    cout << fixed << setprecision(1) << res;
}

汇总代码提交

效率不错!

#include<bits/stdc++.h>
using namespace std;
#define MaxN 100001
int N;
double p, r;
vector<int> node[MaxN];
queue<int> Q;

//这道题仍然是以0为根节点,而结束的结点它也告诉你了,在个数为0的时候作结束。
//在结点为0的时候,它最终给出它的大小(就是这条线得到的商品数量),而销售额就是商品数量*商品价格*增长率
vector<pair<int, int>>mp;
vector<double>rr(MaxN, 1); //存储每个结点得到的增长利润
void Input() {
    ios::sync_with_stdio(false);
    cin >> N >> p >> r;
    for (int i = 0; i < N; i++) { /*建树*/
        int t; cin >> t;
        if (t)
            while (t--) {
                int n; cin >> n; //更新它的子节点
                node[i].emplace_back(n);
            }
        else { //记录结束的结点以及它的购买数量
            int n ; cin >> n;
            mp.emplace_back(make_pair(i, n));
        }
    }
}

void bfs() {
    Q.push(0); //利用bfs得出层级更新利率rr
    while (!Q.empty()) {
        int x = Q.front(); Q.pop();
        for (auto && t : node[x]) {
            rr[t] = rr[x] + rr[x] * (r / 100.0);
            Q.push(t);
        }
    }
}
void print() {
    bfs();
    double res = 0;
    for (auto && t : mp) {
        res += rr[t.first] * (double)t.second * p;
    }
    cout << fixed << setprecision(1) << res;
}
int main() {
    Input();
    print();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值