1106. Lowest Price in Supply Chain (25)

Source: Internet
Author: User

A topic on pat. The original question test instructions is the supply tree structure of a given supplier to a retailer, each of which increases by R at the first supplier price, the price offered by the initial supplier is p, the lowest price to reach the retailer, and the minimum number of retailers. is actually the shortest path to reach the leaf node. Depth-first search can be resolved, but requires proper pruning and records the shortest number of paths, easily timed out. The use of breadth first rather faster and more concise, because the shortest path of the leaf node must be on the same layer, the number of records is also convenient. The code is as follows:

1106.
Lowest Price in supply Chain (+). CPP: The entry point that defines the console application.
#include "stdafx.h" #include <vector> #include <queue> #include <math.h> #include <stdio.h>
using namespace Std;
    int main () {int n;
    Double p,r;
    scanf ("%d%lf%lf", &n,&p,&r);
    Vector<vector<int>> adjsupplier (n);
        for (int i=0;i<n;++i) {int k;
        scanf ("%d", &k);
            for (int j=0;j<k;++j) {int t;
            scanf ("%d", &t);
        Adjsupplier[i].push_back (t);
    }} int levels = 0;
    queue<int>q;
    Q.push (0);
    BOOL flag = FALSE;
    int num = 0;
        while (Q.empty () ==false&&!flag) {queue<int>h;
            while (Q.empty () ==false) {int t = Q.front ();
            Q.pop ();
                if (Adjsupplier[t].size () ==0) {flag = true;
            num++;
  } for (int i=0;i<adjsupplier[t].size (); ++i)          {H.push (adjsupplier[t][i]);
        }} if (flag) break;
        ++levels;
            while (H.empty () ==false) {Q.push (H.front ());
        H.pop ();
    }} Double ans = P*pow ((1+r/100.0), double (levels));
    printf ("%.4lf%d\n", ans,num);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.