1079. Total Sales of Supply Chain (25)

1.使用数的结构存储供应商和零售商

2.采用层次遍历(BFS)进行遍历

3.需要使用double进行存储,一开始发现下面几个耗时大的测试点不通过,所以考虑是精度不够或者溢出的原因,于是改为double类型,然后就通过了



AC代码:

//#include<string>
//#include <iomanip>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;

struct Node{
	vector<int> list;
	bool isRetailer;
	int productCount;
	Node() :list(0), isRetailer(false), productCount(0){};
};
int main(void)
{
	int n;
	double rootPrice, higher;//需要使用double
	cin >> n >> rootPrice >> higher;
	vector<Node> chain(n);
	for (int i = 0; i < n; i++)
	{
		int tmp;
		cin >> tmp;
		if (tmp == 0)
		{//零售商
			chain[i].isRetailer = true;
			cin >> chain[i].productCount;
		}
		else
		{//供应商
			for (int j = 0; j < tmp; j++)
			{//输入下级供应商
				int nextLevel;
				cin >> nextLevel;
				chain[i].list.push_back(nextLevel);
			}
		}
	}

	queue<int> q;
	q.push(0);
	int count1 = 1, count2 = 0;
	double price = rootPrice;
	double totalSales = 0;
	//进行层次遍历
	while (!q.empty())
	{
		for (int i = 0; i < count1; i++)
		{
			int head = q.front(); q.pop();
			if (chain[head].isRetailer)
				totalSales += chain[head].productCount*price;
			else
			{
				for (int j = 0; j < chain[head].list.size(); j++)
				{
					q.push(chain[head].list[j]);
					count2++;
				}
			}
		}
		count1 = count2;
		count2 = 0;
		price *= (1 + higher / 100.0);
	}
	printf("%.1lf\n", totalSales);

	return 0;
}


posted @ 2015-11-24 18:26  siukwan  阅读(166)  评论(0)    收藏  举报