功夫传人 BFS

不算太难
一开始我想的是并查集 结果并着并着成了广搜 我看有人用的是并查集和DFS
我觉得BFS好理解一点
很快我就到13分了。。。结果被剩下了卡了一下午
最后发下是r 算的不对 又上了不认真看题的当。。。我以为是r直接乘代
没想到样例也过。。。 其实是r*代数个r
“每向下传承一代,就会减弱r%”

#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct node
{
    int i, t;
};
int main()
{
    int n;
    double k, r;
    cin >> n >> k >> r;
    r = 1 - r / 100; //转换成百分比
    //存每个人的徒弟
    vector<node> popil[n];
    //吃了多少大还丹
    int a[100005] = {0};
    //这个门派有可能人丁忒少 就剩了他一个
    if (n == 1)
    {
        int x;
        cin >> x >> x;
        cout << (int)(x * k);
        return 0;
    }
    //存
    for (int i = 0; i < n; ++i)
    {
        int m;
        cin >> m;
        if (m == 0)
        {
            cin >> m;
            //存下大还丹的数量
            a[i] = m;
        }
        else
            while (m--)
            {
                int x;
                cin >> x;
                //node 里的i是徒弟 t是辈数
                popil[i].push_back({x, 0});
            }
    }
    //BFS
    queue<node> q;
    double ans[n];
    ans[0] = k;
    q.push({0, 0});
    double sum = 0;
    while (!q.empty())
    {
        node x = q.front();
        q.pop();
        if (popil[x.i].empty())
        {
            if (a[x.i] != 0)
            {
                sum += a[x.i] * (k * pow(r, x.t));
            }
        }
        else
            for (auto &e : popil[x.i])
            {
                e.t = x.t + 1;
                ans[e.i] = k * pow(r, e.t);
                q.push(e);
            }
    }
    cout << (int)(sum);
    return 0;
}

发布了106 篇原创文章 · 获赞 25 · 访问量 7231

猜你喜欢

转载自blog.csdn.net/weixin_45653525/article/details/104173362