大致题意就是求一棵树的所有 叶子节点的权值乘以其对应价格 之和。
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 const int maxn = 100010; 5 6 struct Node { 7 int products; 8 vector<int> child;//孩子结点集合 9 } node[maxn]; 10 11 int n;//结点个数 12 double p,r,ans; 13 14 void DFS(int index,double price) { //当前结点index对应价格是 price 15 if(node[index].child.size() == 0) { //是叶子结点 16 ans += node[index].products * price; 17 return ; 18 } 19 for(int i = 0; i < node[index].child.size(); ++i) { 20 DFS(node[index].child[i],price*(1+r)); 21 } 22 } 23 int main() { 24 cin>>n>>p>>r; 25 r /= 100; //利率是按百分比给出的,记得转换一下!!! 26 for(int i = 0; i < n; ++i) { 27 int k; 28 cin>>k; 29 if(k == 0) 30 cin>>node[i].products;//叶子节点的权值 31 for(int j = 0; j < k; ++j) { 32 int child; 33 cin>>child; 34 node[i].child.push_back(child); 35 } 36 } 37 DFS(0,p);//根结点,层数 0 38 printf("%.1f",ans); 39 return 0; 40 }
1079 Total Sales of Supply Chain
原文:https://www.cnblogs.com/keep23456/p/12395185.html