PAT(甲级)1079

来源:互联网 发布:手机上编程软件 编辑:程序博客网 时间:2024/05/22 18:55

1079. Total Sales of Supply Chain (25)

时间限制
250 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 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 (<=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 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:

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, then instead the total amount of the product will be given after Kj. 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 1010.

Sample Input:
10 1.80 1.003 2 3 51 91 41 70 72 6 11 80 90 40 3
Sample Output:
42.4

#include <iostream>#include <deque>#include <vector>#include <cstring>#define SIZE 100005using namespace std;/////////////////////////////////////////////////////////////the key to success is to use//1.vector<vector<int> > v(SIZE);//2.simplify the representation of tree,eg, just use an array to represent//the node and storethe amounts of products, to those distributors or suppliers//which have its own distributors or retailors,use v[i] to//store them///////////////////////////////////////////////////////////////////deque<int> q;vector<vector<int> > v(SIZE);/*struct Node{Node(int n){number = n;value = 0;Next = NULL;}Node(){number = index++;value = 0;Next = NULL;}int number;int value;static int index;Node *Next;};int Node::index = 0;Node list1[SIZE];double pow(double r,int n){double res =1.0;while(n--){res *=r;}return res;}void display(Node list2[],int n){for(int i=0;i<n;i++){Node *p = &list2[i];while(p != NULL){cout <<p->number <<' ';p = p->Next;}cout <<endl;}}*/int list1[SIZE];int main(){//cout <<sizeof(v) << '\n';int count1,num,N;double p,r;//freopen("test.txt","r",stdin);memset(list1,0,sizeof(list1));scanf("%d%lf%lf",&N,&p,&r);r =r/100+1;double res=0.0;for(int i = 0;i<N;i++){           //creating chain listscanf("%d",&count1);//Node *p=&list1[i];if(count1){for(int j=0;j<count1;j++){scanf("%d",&num);/*Node *pnew = new Node(num);p->Next = pnew;p = p->Next;*/v[i].push_back(num);}}else    scanf("%d",&list1[i]);}//display(list1,N);int level=0;count1 = 0;    q.push_back(0);    q.push_back(-1);    double factor= p;    while(count1 !=N){    int curr = q.front();    if(curr == -1){    level++;    factor *=r;     q.push_back(-1);}else{//Node *p = &list1[curr];//cout <<p->number <<' ';if(list1[curr])    res += list1[curr]*factor;else{/*p =p->Next;    while(p != NULL){    q.push_back(p->number);    p = p->Next; */q.insert(q.end(),v[curr].begin(),v[curr].end());}count1++;}q.pop_front();}printf("%.1lf\n",res);//fclose(stdin);return 0;}

0 0
原创粉丝点击