1090 Highest Price in Supply Chain (25 分)bfs

#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 2e5 + 6;


typedef struct P
{
    int  d;
    vector<int> a;
}P;
P p[maxn];
double qow(double p, int n)
{
    double c = 1;
    while(n)
    {
        if(n & 1)
        {
            c = p * c;
        }
        p = p * p;
        n >>= 1;
    }
    return c;
}
int flag[maxn];
void bfs(double aa, double r)
{
    int t = p[0].a[0];
    queue<int> q;
    q.push(t);
    memset(flag, 0, sizeof(flag));
    flag[t] = 1;
    p[t].d = 0;
    int cnt = 0, m = 0;
    while(!q.empty())
    {
        t = q.front();
        //cout << t << endl;
        q.pop();
        for(int i = 0; i < p[t].a.size(); i++)
        {
            int tt = p[t].a[i];
            if(flag[tt] == 0)
            {
                flag[tt] = 1;
                q.push(tt);
                p[tt].d = p[t].d + 1;
                //cout << tt << " " << p[tt].d << endl;
                if(p[tt].d > m)
                {
                    m = p[tt].d;
                    cnt = 1;
                }
                else if(p[tt].d == m)
                {
                    cnt++;
                }
            }
        }
    }
    printf("%.2f %d\n", aa * qow((1 + r / 100), m), cnt);
}
int main()
{
    int n, x, f;
    double t, r;
    while(scanf("%d%lf%lf", &n, &t, &r) != -1)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &x);
            x++;
            p[x].a.push_back(i);
        }
        if(n == 1)
        {
            printf("%.2f 1", t);
            continue;

        }
        bfs(t, r);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值