B1079 Total Sales of Supply Chain (BFS,DFS)

B1079 Total Sales of Supply Chain (25分)

//第一處bug在price*(1+rate/100),沒看懂題意。
//第二處bug在遞歸邊界處使用price,多加了一次price

price+=(cost[root]*sum);

DFS

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define lowbit(i)((i)&(-i))
using namespace std;
typedef long long ll;
const int MAX=1e5+20;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
const int SQR=632;//633塊,632個
vector<double>vt[MAX];
double price,rate;
double ans=0;
double cost[MAX],book[MAX];
int n;
void dfs(int root,double sum)
{
    if(cost[root]!=0)
    {
        ans+=(cost[root]*sum);
        return ;
    }
    for(int i=0;i<vt[root].size();i++)
        dfs(vt[root][i],sum*rate);
}
int main()
{
    scanf("%d%lf%lf",&n,&price,&rate);
    rate=(rate+100)/100;
    for(int i=0;i<n;i++)
    {
        int k;
        double x;
        scanf("%d",&k);
        if(k)
        {
            for(int j=0;j<k;j++)
            {
                scanf("%lf",&x);
                vt[i].push_back(x);
            }
        }
        else
        {
            scanf("%lf",&x);
            cost[i]=x;
        }
    }
    dfs(0,price);
    printf("%.1f\n",ans);
    return 0;
}

BFS

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define lowbit(i)((i)&(-i))
using namespace std;
typedef long long ll;
const int MAX=1e5+20;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
const int SQR=632;//633塊,632個
vector<double>vt[MAX];
double price,rate;
double ans=0;
double cost[MAX],r[MAX];
int n;
void bfs(int root)
{
    queue<int>q;
    r[root]=price;
    q.push(root);
    while(!q.empty())
    {
        int top=q.front();
        q.pop();
        if(cost[top]!=0)
            ans+=(cost[top]*r[top]);
        for(int i=0;i<vt[top].size();i++)
        {
            int j=vt[top][i];
            r[j]=r[top]*(1+rate);
            q.push(j);
        }
    }
}
int main()
{
    scanf("%d%lf%lf",&n,&price,&rate);
    rate=rate/100;
    for(int i=0;i<n;i++)
    {
        int k;
        double x;
        scanf("%d",&k);
        if(k)
        {
            for(int j=0;j<k;j++)
            {
                scanf("%lf",&x);
                vt[i].push_back(x);
            }
        }
        else
        {
            scanf("%lf",&x);
            cost[i]=x;
        }
    }
    bfs(0);
    printf("%.1f\n",ans);
    return 0;
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容