1079サプライチェーンの総売上高(25ポイント)

1079サプライチェーンの総売上高(25ポイント)

サプライチェーンは、小売業者、流通業者、およびサプライヤのネットワークです。サプライヤから顧客への製品の移動に関係するすべての人です。

あるルートサプライヤーから始めて、チェーンの全員が自分のサプライヤーから価格Pで製品を購入し、Pよりもr%高い価格で製品を販売または配布します。小売業者のみが顧客と向き合います。サプライチェーンの各メンバーには、ルートサプライヤを除いて、サプライヤが1つだけあり、サプライサイクルはないと想定されています。

サプライチェーンが与えられたので、すべての小売業者からの総売上高を伝えることになっています。

入力仕様:
各入力ファイルには、1つのテストケースが含まれています。各場合について、最初の行は、3つの正の数値含ま:N(≤10
5
)、サプライチェーンのメンバの総数を(したがってそれらのIDのは、0からN-1までの番号、およびルート・サプライヤーのさIDは0です); P、ルートサプライヤーによって与えられた単価。r、各ディストリビューターまたは小売業者の価格上昇率。次に、N行が続き、それぞれが次の形式で販売業者または小売業者を表します。

K
i
ID [1] ID [2]…ID [K
i
]

ここで、i番目の行で、K
i
は、サプライヤーiから製品を受け取るディストリビューターまたは小売業者の総数であり、その後にこれらのディストリビューターまたは小売業者のIDが続きます。K
j
が0であるということは、j番目のメンバーが小売業者であることを意味します。代わりに、製品の合計金額は
Kjの後に表示されます
行内のすべての数字はスペースで区切られます。

出力仕様:
テストケースごとに、すべての小売業者から期待できる総売上高を小数点以下1桁まで正確に1行で印刷します。数が10超えないことが保証される
10

サンプル入力:
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
サンプル出力:
42.4
DFS

#include<bits/stdc++.h>
using namespace std;
struct node{
    
    
    double data;
    vector<int> child;
}Node[100010];
int n;
double p,r,total=0;
void DFS(int index,int depth){
    
    
    if(Node[index].child.empty()){
    
    
        total+=Node[index].data*pow(1+r,depth);
    }
    for(int i=0;i<Node[index].child.size();++i)
        DFS(Node[index].child[i],depth+1);
}
int main(){
    
    
    scanf("%d%lf%lf",&n,&p,&r);
    r/=100;
    int k,child;
    for(int i=0;i<n;++i){
    
    
        scanf("%d",&k);
        if(k==0){
    
    
            scanf("%lf",&Node[i].data);
        }else{
    
    
            for(int j=0;j<k;++j){
    
    
                scanf("%d",&child);
                Node[i].child.push_back(child);
            }
        }
    }
    DFS(0,0);
    printf("%.1f\n",p*total);
}

BFS

#include<bits/stdc++.h>
using namespace std;
struct node{
    
    
    double data;
    int depth;
    vector<int> child;
}Node[100010];
int n;
double p,r,total=0;
void BFS(int index){
    
    
    queue<int> q;
    q.push(index);
    Node[index].depth=0;
    while(!q.empty()){
    
    
        int now=q.front();
        if(Node[now].child.empty()){
    
    
            total+=Node[now].data*pow(1+r,Node[now].depth);
        }
        q.pop();
        for(int i=0;i<Node[now].child.size();++i){
    
    
            int child=Node[now].child[i];
            Node[child].depth=Node[now].depth+1;
            q.push(child);
        }
    }
}
int main(){
    
    
    scanf("%d%lf%lf",&n,&p,&r);
    r/=100;
    int k,child;
    for(int i=0;i<n;++i){
    
    
        scanf("%d",&k);
        if(k==0){
    
    
            scanf("%lf",&Node[i].data);
        }else{
    
    
            for(int j=0;j<k;++j){
    
    
                scanf("%d",&child);
                Node[i].child.push_back(child);
            }
        }
    }
    BFS(0);
    printf("%.1f\n",p*total);
}

おすすめ

転載: blog.csdn.net/weixin_44970602/article/details/112443489