思路借鉴了之前的图的层数,发现这个贼好用;关键在于输出浮点数的格式,这个要注意一下
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
using std::vector;
using std::queue;
const int maxn=100010;
struct node{
double data;
vector<int>child;
int layer=0;
}Node[maxn];
int n;
double p,r;
vector<int>leaf;
void BFS(int x){
int layer=0;
queue<int>q;
q.push(x);
while(!q.empty()){
layer++;
int index;
int length=q.size();
for(int i=0;i<length;i++){
index=q.front();
q.pop();
Node[index].layer=layer;
for(int j=0;j<Node[index].child.size();j++){
q.push(Node[index].child[j]);
}
}
}
}
int main(){
int num;
scanf(“%d%lf%lf”,&n,&p,&r);
for(int i=0;i<n;i++){
scanf(“%d”,&num);
if(num==0){
scanf(“%lf”,&Node[i].data);
leaf.push_back(i);
}else{
for(int j=0;j<num;j++){
int child;
scanf(“%d”,&child);
Node[i].child.push_back(child);
}
}
}
BFS(0);
double sum;
for(int i=0;i<leaf.size();i++){
sum+=pow(1+r/100,Node[leaf[i]].layer-1)*Node[leaf[i]].data;
}
printf(“%.1f\n”,sum*p);
system(“pause”);
return 0;
}