1106 Lowest Price in Supply Chain(25 分)

模板题目
DFS:

#include
#include
#include
using namespace std;
const int maxn = 1e5 + 10;
struct node {
    double p;
    vectorchild;
}Node[maxn];
double minp = 1e10 + 10;
int cnt = 0;
int n;
double p, r;
void DFS(int root)
{
    if (Node[root].child.size() == 0)
    {
        if (Node[root].p < minp)minp = Node[root].p, cnt = 1;
        else if (Node[root].p == minp)cnt++;
        return;
    }
    for (int i = 0; i < Node[root].child.size(); i++)
    {
        int child = Node[root].child[i];
        Node[child].p = Node[root].p*(1 + r);
        DFS(child);
    }
}

int main()
{
    scanf("%d%lf%lf", &n, &p, &r);
    r /= 100;
    for (int i = 0; i < n; i++)
    {
        int k, x;
        scanf("%d", &k);
        while (k--)
        {
            scanf("%d", &x);
            Node[i].child.push_back(x);
        }
    }
    Node[0].p = p;
    DFS(0);
    printf("%.4f %d", minp, cnt);
    return 0;
}

BFS:

void BFS(int root)
{
    queueq;
    q.push(root);
    while (!q.empty())
    {
        int top = q.front();
        q.pop();
        if (Node[top].child.size())
        {
            for (int i = 0; i < Node[top].child.size(); i++)
            {
                int child = Node[top].child[i];
                Node[child].p = Node[top].p*(1 + r);
                q.push(child);
            }
        }
        else
        {
            if (Node[top].p < minp)minp = Node[top].p, cnt = 1;
            else if (Node[top].p == minp)cnt++;
        }
    }
}
#include
using namespace std;
const int maxn=1e5+10;
vectora[maxn];
int n;
double P,r;
int minheight=maxn,ans;
void DFS(int v,int height)
{
    if(a[v].size()==0)
    {
        if(height

你可能感兴趣的:(1106 Lowest Price in Supply Chain(25 分))