本文主要是介绍PTA甲级 1106 Lowest Price in Supply Chain (25分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬
本文由参考于柳神博客写成
柳神的CSDN博客,这个可以搜索文章
柳神的个人博客,这个没有广告,但是不能搜索
还有就是非常非常有用的 算法笔记 全名是
算法笔记 上级训练实战指南 //这本都是PTA的题解
算法笔记
PS 今天也要加油鸭
题目原文
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:
K**i ID[1] ID[2] … ID[K**i]
where in the i-th line, K**i 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. K**j 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
生词如下:
都看懂了.就是要你算出最短的度
思路如下:
dfs遍历一遍就OK
测试点3 测试点4 报错的原因
就是你的Min就是最大的度开的太小的缘故
代码如下:
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
struct node {int data;vector<int> child;
};
vector<node> v;
int Max = 99999999,ans=0;
void dfs(int index, int depth) {if (v[index].child.size() == 0) {if (depth < Max) {Max = depth;ans = 1;}else if (depth == Max) ++ans;return;}for (int i = 0; i < v[index].child.size(); ++i) dfs(v[index].child[i], depth + 1);
}
int main(void) {double p=0, r=0;int n=0,t=0,k=0;scanf("%d%lf%lf", &n, &p, &r);v.resize(n);for (int i = 0; i < n; ++i) {scanf("%d", &t);for (int j = 0; j < t; ++j) {scanf("%d", &k);v[i].child.push_back(k);}}dfs(0, 0);printf("%.4lf %d\n", p*pow(1+r/100.0,Max),ans);return 0;
}
这里柳神的代码和我的基本一模一样,就不贴了.
不一样的是,她用的是一个vector我用了node
还是贴一下吧.
柳神代码如下:
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
vector<int> v[100005];
int mindepth = 99999999, minnum = 1;
void dfs(int index, int depth) {if(mindepth < depth)return ;if(v[index].size() == 0) {if(mindepth == depth)minnum++;else if(mindepth > depth) {mindepth = depth;minnum = 1;}}for(int i = 0; i < v[index].size(); i++)dfs(v[index][i], depth + 1);
}
int main() {int n, k, c;double p, r;scanf("%d %lf %lf", &n, &p, &r);for(int i = 0; i < n; i++) {scanf("%d", &k);for(int j = 0; j < k; j++) {scanf("%d", &c);v[i].push_back(c);}}dfs(0, 0);printf("%.4f %d", p * pow(1 + r/100, mindepth), minnum);return 0;
}
如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗
相信我,你也能变成光.
如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.
这篇关于PTA甲级 1106 Lowest Price in Supply Chain (25分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!