已经出现了很多次这种题了,做法也都是一样。
#include <iostream>
#include <queue>
#include <unordered_map>
#include <cmath>
using namespace std;
int n,k,x;
double p,r,ans;
vector<int>e[100010];
unordered_map<int,int>mp,cnt;
void bfs()
{
queue<pair<int,int>>que;
que.push({0,0});
while(que.size())
{
auto t = que.front();que.pop();
int u = t.first,c = t.second;
if(e[u].size() == 0) cnt[u] = c;
for(auto it : e[u]) que.push({it,c + 1});
}
}
int main()
{
cin >> n >> p >> r;
for(int i = 0; i < n; i ++)
{
cin >> k;
if(k == 0)
{
cin >> x;
mp[i] = x;
}
else
while(k --)
{
cin >> x;
e[i].push_back(x);
}
}
bfs();
for(auto it : cnt)
ans += (p * pow(1 + r / 100,it.second)) * mp[it.first];
printf("%.1lf",ans);
return 0;
}