zoukankan      html  css  js  c++  java
  • PAT L2-020 功夫传人【BFS】

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

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

    输入格式:

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

    Ki ID[1] ID[2] ... ID[Ki]

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

    输出格式:

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

    输入样例:

    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
    

     这题有一个数据卡了我好久,原来是祖师爷也有可能是得道者。。

    附ac代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <map>
     8 #include <queue>
     9 #include <map>
    10 #include <cmath>
    11 using namespace std;
    12 const int inf = 0x3f3f3f3f;
    13 typedef long long ll;
    14 const int maxn = 1e5+10;
    15 vector<int>nu[maxn];
    16 int f[maxn];
    17 double level[maxn];
    18 bool vis[maxn];
    19 queue<int>q;
    20 int n;
    21 double z,r;
    22 void bfs()
    23 {
    24     q.push(0);
    25     while(!q.empty())
    26     {
    27         int u=q.front();q.pop();
    28         for(int i=0;i<nu[u].size();++i)
    29         {
    30             int v=nu[u][i];
    31             level[v]=level[u]*(1.0-r/100.0);
    32             if(f[v]) level[v]*=nu[v][0];
    33             else q.push(v);
    34         }
    35     }
    36 }
    37 int main()
    38 {
    39     
    40     scanf("%d%lf%lf",&n,&level[0],&r);
    41     int k,u;
    42     for(int i=0;i<n;++i)
    43     {
    44         scanf("%d",&k);
    45         if(k==0)
    46         {
    47             scanf("%d",&u);
    48             f[i]=1;
    49             nu[i].push_back(u);
    50         }
    51         for(int j=0;j<k;++j)
    52         {
    53             scanf("%d",&u);
    54             nu[i].push_back(u);
    55         }
    56     }
    57     if(f[0])//特判
    58     {
    59         printf("%d",int(level[0]*nu[0][0]));
    60         return 0;
    61     }
    62     bfs();
    63     double ans=0.0;
    64     for(int i=0;i<maxn;++i)
    65         if(f[i])
    66         ans+=level[i];
    67     printf("%d",int(ans));
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    Linux日志管理系统rsyslog
    Linux访问权限控制及时间同步实践
    Linux系统自动化安装之cobbler实现
    【转】java取整和java四舍五入方法
    The web application [ ] registered the JDBC driver [net.sourceforge.jtds.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver
    Python学习笔记之函数式编程
    Java去重字符串的两种方法以及java中冒号的使用
    Python学习之字符串格式化
    Python学习之文件操作
    Python学习笔记之爬虫
  • 原文地址:https://www.cnblogs.com/zmin/p/8544144.html
Copyright © 2011-2022 走看看