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; }