Pat 1106. Lowest Price in Supply Chain (25) __ The most shallow leaf nodes

Source: Internet
Author: User

A supply chain is a network of retailers (retailer), distributors (reseller), and suppliers (suppliers)--everyone involved in moving a produ CT from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one ' s supplier in a price P and sell or Distribu Te them in a and is r% higher than P. Only the retailers would face the customers. It is assumed this supply chain has exactly one supplier except the root supplier, and there is no supp Ly cycle.

Now given a supply chain, your are supposed to tell the lowest price a customer can expect from some retailers.

Input Specification:

Each input file contains one test case. For the contains three positive numbers:n (<=105), the total number of the ' members ' in the supply Chain (and hence their ID ' s are numbered from 0 to N-1, and the root supplier ' s ID is 0); P, the price given by the root supplier; and r, the percentage rate of price increment to each distributor or retailer. Then N lines Follow, each describes a distributor or retailer in the following format:

Ki id[1] id[2] ... Id[ki]

Where in the i-th line, the "Ki is" total number of distributors or retailers who receive all products from supplier I, and is T Hen followed by the ID ' s of these distributors or retailers. Kj being 0 means the j-th member is a retailer. All of the numbers in a line are separated by a.

Output Specification:

For each test case, print at one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, And the number of retailers that sell in the lowest price. There must is one space between the two numbers. It is guaranteed the "all" prices won't exceed 1010. Sample Input:

1.80 1.00
3 2 3 5
1 9
1 4 1 7
0 2 6 1 1 8 0 0 0
Sample Output:
1.8362 2

Recently did the topic of the binary tree become addicted.

Key point: Build, find out the depth of the most shallow leaf node and its number can be.

#include <iostream>
#include <string.h>
#include <stack>
#include <vector>
using namespace std;
#include <stdio.h>
#include <algorithm>
#include <queue>
vector<int>tree[ 150000];
int n,mm,ans[150000];
Double p,r,t;
void Dfs (int u,int depth)
{
    int i;
  if (Tree[u].size () ==0)
  {
      mm=min (mm,depth);
      ans[depth]++;
      return;
  }
  For (I=0;i<tree[u].size (); i++)
  {
     dfs (tree[u][i],depth+1);
  }
}
int main ()
{
    int i,k,j,num;
   cin>>n>>p>>r;
    for (i=0;i<n;i++)
    {
      cin>>k;
     for (j=0;j<k;j++)
      {
       cin>>num;
       Tree[i].push_back (num);
      }
    }
    mm=999999;
    DFS (0,1);
     T=1;
    for (i=1;i<mm;i++)
     t=t* (1+r*1.0/100);
     printf ("%.4lf%d\n", t*p,ans[mm]);
    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.