1079 Total Sales of Supply Chain (25 分)(深度优先搜索树的遍历(二维向量矩阵))

1079 Total Sales of Supply Chain (25 分)

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 total sales from all the retailers.

Input Specification:

Each input file contains one test case. For each case, the first line contains three positive numbers: N (≤10​5​​), 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 unit 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, then instead the total amount of the product will be given after K​j​​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 10​10​​.

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3

Sample Output:

42.4

树的遍历:

/**
本题 金钱递增是R  如果有n个结点 (1+r) * (1+r) * . . .(n个) != (1+r*n)
此题思路就是存储下起点到所有临售站的路径(构成了一颗树)通过深度优先搜索将所有路径的值计算  
**/

方法一:

/**
本题 金钱递增是R  如果有n个结点 (1+r) * (1+r) * . . .(n个) != (1+r*n)
此题思路就是存储下起点到所有临售站的路径(构成了一颗树)通过深度优先搜索将所有路径的值计算  
**/ 
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
struct Node{
	int k;
	int to;
	int cost;
	Node(int to, int k,int costt=0.0): k(k), to(to), cost(costt){ }   //带有默认参数的变量需要放在后面 
};
int n, to, k, cost, num;
double p, r, sum;
vector<vector<Node>> a(100001); 
void dfs(int start,int num){
	
	if(a[start][0].k == 0) {
		sum +=  pow(r+1, num) * p * a[start][0].cost;
		return;
	}
	for(int i = 0; i < a[start].size(); i++){
		dfs(a[start][i].to, num + 1); // num + 1保留了 num原地址值,num++不会保存 
		
	}
	
}
int main(){
	scanf("%d%lf%lf", &n, &p, &r);
	r = r / 100;
	for(int i = 0; i < n; i++){
		scanf("%d", &k);
		if(k==0){
			scanf("%d", &cost);
			a[i].push_back(Node(i, k, cost));
		}else{
			for(int j = 0; j < k; j++){
				scanf("%d", &to);
				a[i].push_back(Node(to, k));
			}	
	   }
	}
	dfs(0);
	printf("%.1f",sum);
	return 0;
}

方法二: 运用c++内积计算方法: 

思路:/**
inner_product(beg1,end1,beg2,init); https://blog.csdn.net/richenyunqi/article/details/80139424
beg1,end1表示第一个序列的首尾迭代器,beg2表示第二个序列的首迭代器,进行处理的元素个数与第一个序列相同,
来自两个序列的对应元素相乘并累加起来,和的初值由init指定,并返回的最终的和,最终的和的类型由init指定。
**/

#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
const int maxsize = (int)1e5+5; //科学计数法,e代表10 表示 10的6次方 
//vector<vector<int>> path(maxsize); //全局定义好的常量可以赋范围 
vector<int> path[maxsize]; 
int n,k,to,product[maxsize];
double price[maxsize], r, sum;
 
void dfs(int start){    //深度遍历 ,将临售站最终金钱 * 润率计算出来 
	for(int i = 0; i < path[start].size(); i++){
		price[path[start][i]] = price[start] * (1 + r / 100);
		dfs(path[start][i]);
	}
}
int main(){
	scanf("%d%lf%lf", &n, &price[0], &r);
	for(int i = 0; i < n; i++){
		scanf("%d", &k);
		if(k == 0){
			scanf("%d", &product[i]);
		}
		while(k--){
			scanf("%d", &to);
			path[i].push_back(to);
		}
//		for(int i = 0; i < k; i++){ //这里用for循环为41.8.. 
//			scanf("%d", &to);
//			path[i].push_back(to);
//		}
	}
	dfs(0);
//	sum = inner_product(price, price+n, product, 0.0);
//	for(int i = 0; i < n; i++){
//		sum += product[i] * price[i];
//	}
	printf("%.1f", inner_product(price, price+n, product, 0.0));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41698081/article/details/91126193
今日推荐