PAT甲级真题及训练集(26)--1106. Lowest Price in Supply Chain (25)
2017-07-09 17:44
411 查看
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
提交代码
/** 作者:一叶扁舟 时间:17:25 2017/7/9 思路:统计每一层叶子结点的个数,计算最短路径的长度,计算最短路径结点个数,并输出最少价格,本题站在第A1079的基础上修改的部分代码,(虽然A1079并未全部通过,思路是对的) */ #include <stdio.h> #include <stdlib.h> #include <algorithm> #include <stack> #include <queue> #include <math.h> #include <string.h> #include <vector> using namespace std; #define SIZE 100001 typedef struct TreeNode{ int data; vector<int> child; }TreeNode; //定义的树,可以用结构体,也可以用直接用vector<int> tree来定义 TreeNode tree[SIZE]; int root[SIZE] = { 0 };//0默认是根节点,1是子结点 int level[SIZE] = { 0 };//定义每一层统计树叶子节点数 int minLevelNum = SIZE;//最小层叶子结点个数 void getTreeDepth(int rootNum, int depth){ //到了叶子节点 if (tree[rootNum].child.size() == 0){ if (minLevelNum > depth){ minLevelNum = depth; } level[depth]++;//每一层叶子结点个数 return; } for (unsigned int i = 0; i < tree[rootNum].child.size(); i++){ //递归访问root的子结点 getTreeDepth(tree[rootNum].child[i], depth + 1); } } int getRoot(int N){ for (int i = 0; i < N; i++){ if (root[i] != 1){ return i; } } return 0; } int main(){ int N; double m;//价格增加的倍数 double r;//价格每次增加的比率 scanf("%d %lf %lf", &N, &m, &r); r = r / 100; for (int i = 0; i < N; i++){ int num; scanf("%d", &num); if (num == 0){//叶子节点 }else{ for (int j = 0; j < num; j++){ int temp; scanf("%d", &temp); root[temp] = 1;//子结点再这里出现了,说明不可能是根节点 //存入对应的孩子结点 tree[i].child.push_back(temp); } } } //得到根结点 int rootNum = getRoot(N); getTreeDepth(rootNum, 0); //计算最小价格 double result = m * pow(1 + r, minLevelNum); printf("%.4f %d\n", result, level[minLevelNum]); system("pause"); return 0; }
相关文章推荐
- 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)
- 1106. Lowest Price in Supply Chain (25) PAT甲级
- PAT甲级真题及训练集(22)--1090. Highest Price in Supply Chain (25)
- PAT - 甲级 - 1106. Lowest Price in Supply Chain (25)(多叉树)
- PAT 甲级 1106. Lowest Price in Supply Chain (25)
- PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
- pat 1106. Lowest Price in Supply Chain (25)
- PAT (Advanced Level) Practise 1106 Lowest Price in Supply Chain(25)
- PAT (Advanced Level) Practise 1106 Lowest Price in Supply Chain (25)
- PAT 1106. Lowest Price in Supply Chain (25) 单入度树 + 求叶子到树的距离+反向递归
- pat 1106. Lowest Price in Supply Chain (25)
- 【PAT】【Advanced Level】1106. Lowest Price in Supply Chain (25)
- PAT 1106. Lowest Price in Supply Chain (25)
- PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
- Pat(A) 1106. Lowest Price in Supply Chain (25)
- PAT_A 1106. Lowest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)-PAT甲级真题
- 1090. Highest Price in Supply Chain (25)-PAT甲级真题
- 【PAT】1106. Lowest Price in Supply Chain (25)
- [建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)