PAT 1106 Lowest Price in Supply Chain

1106. Lowest Price in Supply Chain (25)

時間限制
200 ms
內存限制
65536 kB
代碼長度限制
16000 B
判題程序
Standard
作者
CHEN, Yue

A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involved in moving a product 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 distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

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

Input Specification:

Each input file contains one test case. For each case, The first line 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 for 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, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID's of these distributors or retailers. Kj being 0 means that the j-th member is a retailer. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1010.

Sample Input:
10 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
題目:求一棵樹葉節點中層數最低的層數,和有幾個最低層數葉節點
思路:
建樹,給每個節點賦層數值,遍歷所有葉節點,找層數最低的
啓示:
給樹的每一個節點賦層數值,最好單獨用層次遍歷一個個賦,一邊建樹一邊賦值不靠譜

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stdio.h>
#include <math.h>

#define MAXN 100002

struct TreeNode {
  int value;
  TreeNode* first_child;
  TreeNode* next_sibling;
  TreeNode* parent;
  int level;
  bool is_leaves;
};

int main() {
  int num_nodes;
  double root_price, increase_rate;
  TreeNode* nodes[MAXN];
  std::vector<TreeNode*> leaves;
  std::vector<TreeNode*> unleveled_nodes;
  scanf("%d %lf %lf", &num_nodes, &root_price, &increase_rate);
  for (int i = 0; i < num_nodes; i++) {
    nodes[i] = (TreeNode*)malloc(sizeof(TreeNode));
    nodes[i]->parent = NULL;
    nodes[i]->value = i;
    nodes[i]->first_child = NULL;
    nodes[i]->next_sibling = NULL;
    nodes[i]->is_leaves = false;
    nodes[i]->level = -1;
    if (i == 0) {
      nodes[i]->level = 0;
    }
  }
  for (int i = 0; i < num_nodes; i++) {
    int k;
    std::cin >> k;
    if (k > 0) {
      int fc;
      std::cin >> fc;
      nodes[i]->first_child = nodes[fc];
      nodes[fc]->parent = nodes[i];
      nodes[fc]->level = nodes[i]->level + 1;
      TreeNode* p = nodes[fc];
      for (int j = 0; j < k - 1; j++) {
        int sb;
        std::cin >> sb;
        p->next_sibling = nodes[sb];
        nodes[sb]->parent = nodes[i];
        p = p->next_sibling;
        if (nodes[i]->level != -1) {
          p->level = nodes[i]->level + 1;
        }
        else {
          unleveled_nodes.push_back(nodes[i]);
          unleveled_nodes.push_back(p);
        }
      }
    }
    else {
      nodes[i]->is_leaves = true;
      leaves.push_back(nodes[i]);
    }
  }
  std::queue<TreeNode*> q;
  q.push(nodes[0]);
  while (!q.empty()) {
    TreeNode* t = q.front();
    q.pop();
    TreeNode* f = t->first_child;
    while (f) {
      q.push(f);
      f->level = t->level + 1;
      f = f->next_sibling;
    }
  }
  int count = 0;
  int min_level = MAXN;
  for (int i = 0; i < leaves.size(); i++) {
    TreeNode* node = leaves[i];
    if (min_level > node->level) {
      count = 1;
      min_level = node->level;
    }
    else if (min_level == node->level) {
      count++;
    }
  }
  // printf("min_level is %d\n", min_level);
  printf("%.4f %d", root_price * pow((1+increase_rate/100), min_level), count);
  system("pause");
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章