PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)

本文介绍了一个基于深度优先搜索(DFS)的算法实现案例,通过该算法可以求解特定路径问题。文章提供了完整的C++代码实现,并详细展示了如何利用DFS遍历图结构来找到最优解及其数量。

简单dfs

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
vector<int>g[maxn];
int n;
double P,r;
int ans_count=0;
double ans_price=99999999;

void dfs(int x,double price)
{
    if(g[x].size()==0)
    {
        if(price<ans_price)
        {
            ans_count=1;
            ans_price=price;
        }
        else if(price==ans_price) ans_count++;
        return;
    }

    for(int i=0;i<g[x].size();i++)
    {
        dfs(g[x][i],price*(1+r/100));
    }
}

int main()
{
    scanf("%d",&n);
    scanf("%lf%lf",&P,&r);
    for(int i=0;i<n;i++)
    {
        int num; scanf("%d",&num);
        while(num--)
        {
            int x; scanf("%d",&x);
            g[i].push_back(x);
        }
    }

    dfs(0,P);

    printf("%.4lf %d\n",ans_price,ans_count);

    return 0;
}

 

转载于:https://www.cnblogs.com/zufezzt/p/5642481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值