pat1079. Total Sales of supply Chain (25)

Source: Internet
Author: User

1079. Total Sales of supply Chain (25) time limit of MS memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Chen, Yue

A supply chain is a network of retailers (retailer), distributors (dealer), and suppliers (supplier)--everyone involved in moving a produ CT 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 Distribu Te them in a price, is r% higher than P. Only the retailers would face the customers. It is assumed this member in the supply chain have exactly one supplier except the root supplier, and there is no supp Ly cycle.

Now given a supply chain and you is supposed to tell the total sales from all the retailers.

Input Specification:

Each input file contains the 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 is 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 was the total number of distributors or retailers who receive products from supplier I, and are T Hen followed by the ID ' s of these distributors or retailers. Kj being 0 means that the j-th member was a retailer, then instead the total amount of the product would be given after Kj. All the numbers in a line is 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 the number would not be 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

Submit Code

BFS. Because the number is a long long level, the DFS segment is incorrect.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <stack>5#include <vector>6#include <Set>7#include <map>8#include <queue>9 using namespacestd;Tenmap<Long Long,vector<Long Long> >Edge; Onemap<Long Long,Long Long>re; A intMain () { -     //freopen ("D:\\input.txt", "R", stdin); -     Long Longn,i,num,v; the     DoublePrice,r,total=0; -scanf"%lld%lf%lf",&n,&price,&R); -      for(i=0; i<n;i++){ -scanf"%lld",&num); +         if(!num) { -scanf"%lld",&re[i]); +             Continue; A         } at          while(num) { -scanf"%lld",&v); - Edge[i].push_back (v); -num--; -         } -     } inqueue<Long Long>Q; -Q.push (0); to     Long Longcur; +     Long LongLast,e=0; -     //Last record node where the current layer is not a leaf node the     if(Re.count (0)){//Root may also be sold directly to customers *total+=price*re[0]; $ Q.pop ();Panax Notoginseng     } -r=r/ -; theprice*=1+r;//current price for the next level of takeaway +      while(!Q.empty ()) { ACur=Q.front (); the Q.pop (); +          for(i=0; I<edge[cur].size (); i++){ -             if(Re.count (Edge[cur][i])) { $total+=price*Re[edge[cur][i]]; $                 Continue; -             } - Q.push (Edge[cur][i]); thelast=Edge[cur][i]; -         }Wuyi         if(cur==e) { theE=Last ; -price*=1+R; Wu         } -     } Aboutprintf"%.1lf\n", total); $     return 0; -}

The error is broken by DFS.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <stack>5#include <vector>6#include <Set>7#include <map>8 using namespacestd;9map<Long Long,vector<Long Long> >Edge;Tenmap<Long Long,Long Long>re; Onemap<Long Long,BOOL>Vis; A voidDFS (Long LongNumDouble&total,DoublePriceDoubleR) { -vis[num]=true; -     Long Longi; the     if(!edge[num].size ()) { -  -         //cout<<num<<endl; -  +total+=re[num]*Price ; -         return; +     } A      for(i=0; I<edge[num].size (); i++){ at         if(!Vis[edge[num][i]]) { -DFS (edge[num][i],total,price* (1+R), R); -         } -     } - } - intMain () { in     //freopen ("D:\\input.txt", "R", stdin); -     Long Longn,i,num,v; to     DoublePrice,r,total=0; +scanf"%lld%lf%lf",&n,&price,&R); -      for(i=0; i<n;i++){ thevis[i]=false; *scanf"%lld",&num); $         if(!num) {Panax Notoginsengscanf"%lld",&re[i]); -             Continue; the         } +          while(num) { Ascanf"%lld",&v); the Edge[i].push_back (v); +num--; -         } $     } $Daso0, total,price,r/ -); -printf"%.1lf\n", total); -     return 0; the}

pat1079. Total Sales of supply Chain (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.