天梯赛L2-020 功夫传人(BFS DFS两种办法超详细)

L2-020 功夫传人 (25分)
一门武功能否传承久远并被发扬光大,是要看缘分的。一般来说,师傅传授给徒弟的武功总要打个折扣,于是越往后传,弟子们的功夫就越弱…… 直到某一支的某一代突然出现一个天分特别高的弟子(或者是吃到了灵丹、挖到了特别的秘笈),会将功夫的威力一下子放大N倍 —— 我们称这种弟子为“得道者”。

这里我们来考察某一位祖师爷门下的徒子徒孙家谱:假设家谱中的每个人只有1位师傅(除了祖师爷没有师傅);每位师傅可以带很多徒弟;并且假设辈分严格有序,即祖师爷这门武功的每个第i代传人只能在第i-1代传人中拜1个师傅。我们假设已知祖师爷的功力值为Z,每向下传承一代,就会减弱r%,除非某一代弟子得道。现给出师门谱系关系,要求你算出所有得道者的功力总值。

输入格式:
输入在第一行给出3个正整数,分别是:N(≤10
​5
​​ )——整个师门的总人数(于是每个人从0到N−1编号,祖师爷的编号为0);Z——祖师爷的功力值(不一定是整数,但起码是正数);r ——每传一代功夫所打的折扣百分比值(不超过100的正数)。接下来有N行,第i行(i=0,⋯,N−1)描述编号为i的人所传的徒弟,格式为:

K
​i
​​ ID[1] ID[2] ⋯ ID[K
​i
​​ ]

其中K
​i
​​ 是徒弟的个数,后面跟的是各位徒弟的编号,数字间以空格间隔。K
​i
​​ 为零表示这是一位得道者,这时后面跟的一个数字表示其武功被放大的倍数。

输出格式:
在一行中输出所有得道者的功力总值,只保留其整数部分。题目保证输入和正确的输出都不超过10
​10
​​ 。

输入样例:

10 18.0 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

输出样例:

404

分析:求出所有得道者的的功力总值。 visit判断谁是得道者

BFS结构体
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
struct node{
    int num;
    double weight;//注意定义double类型
};
vector<vector<int> > v;
int n, k;
double z, r, result = 0.0;
bool visit[100010];
void bfs(int index) {
    queue<node> q;
    node temp;
    temp.num=index;
    temp.weight=z;
    q.push(temp);
    while(!q.empty()){
     node top=q.front();
        q.pop();
    if (visit[top.num] == true) {
        result += top.weight*(v[top.num][0]);
        continue;//进行下一步
    }
    for (int j = 0; j < v[top.num].size(); j++)
       q.push({v[top.num][j],top.weight*r});
   }
}
int main() {
    scanf("%d%lf%lf", &n, &z, &r);
    v.resize(n);
    r=1 - r/100;
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        int temp;
        if (k == 0) {
            scanf("%d", &temp);
            v[i].push_back(temp);
            visit[i] = true;
        }
        for (int j = 0; j < k; j++) {
            scanf("%d", &temp);
            v[i].push_back(temp);
        }
     }
    bfs(0);
    printf("%d", (int)result);
    return 0;
}

BFS数组
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

vector<vector<int> > v;
int n, k;
double z, r, result = 0.0;
bool visit[100010];
double g[100010]={0};
void bfs(int index) {
    queue<int> q;
    g[index]=z;
    q.push(index);
    while(!q.empty()){
     int top=q.front();
        q.pop();
    if (visit[top] == true) {
        result += v[top][0]*g[top];
        continue;
    }
    for (int j = 0; j < v[top].size(); j++){
       q.push(v[top][j]);
        g[v[top][j]]=g[top]*r;
   }
 }
}
int main() {
    scanf("%d%lf%lf", &n, &z, &r);
    v.resize(n);
    r=1 - r/100;
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        int temp;
        if (k == 0) {
            scanf("%d", &temp);
            v[i].push_back(temp);
            visit[i] = true;
        }
        for (int j = 0; j < k; j++) {
            scanf("%d", &temp);
            v[i].push_back(temp);
        }
     }
    bfs(0);
    printf("%d", (int)result);
    return 0;
}

DFS
#include <cstdio>
#include <vector>
using namespace std;
vector<vector<int> > v;
int n, k;
double z, r, result = 0.0;
bool visit[100010];
void dfs(int index, double power) {
    if (visit[index] == true) {
        result += power * v[index][0];
        return;
    }
    for (int i = 0; i < v[index].size(); i++)
        dfs(v[index][i], power * (1 - r/100));
}
int main() {
    scanf("%d%lf%lf", &n, &z, &r);
    v.resize(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        int temp;
        if (k == 0) {
            scanf("%d", &temp);
            v[i].push_back(temp);
            visit[i] = true;
        }
        for (int j = 0; j < k; j++) {
            scanf("%d", &temp);
            v[i].push_back(temp);
        }
    }
    dfs(0, z);
    printf("%d", (int)result);
    return 0;
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王子y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值