PAT 1079. Total Sales of Supply Chain (25) BFS+快速幂

来源:互联网 发布:淘宝直通车是在哪里找 编辑:程序博客网 时间:2024/05/21 09:55

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


一开始只能过前两个,中间三个答案错误,最后一个超时。检查无误,改float为double,前5个通过,最后一个超时。改为scanf,依然超时。BFS无法再优化,遂注意到次方问题,改为快速幂。


#include<iostream>#include<string>#include<vector>#include<queue>#include<iomanip>#include<stdio.h>using namespace std;int mark[100001]={0};vector<int> tutu[100001];long long huo[100001]={0};int main(){int n;double p,r;//cin>>n>>p>>r;scanf("%d %lf %lf",&n,&p,&r);mark[0]=0;   for(int i=0;i<n;i++)    {    int tmp;    //cin>>tmp;    scanf("%d",&tmp);    for(int j=0;j<tmp;j++)     {     int tmmp;     //cin>>tmmp;     scanf("%d",&tmmp);     tutu[i].push_back(tmmp);     } if(tmp==0) //cin>>huo[i] ; scanf("%lld",&huo[i]);    } queue<int> duilie; int np=0; double sum=0;  duilie.push(np);  while(!duilie.empty())   {    np=duilie.front();    duilie.pop();    if(tutu[np].empty())    //快速幂   {    //double s=p; //for(int i=0;i<mark[np];i++)  // s*=(r+100)/100; // sum+=s*huo[np];  double quan=(r+100)/100;  double cifang=1;  while(mark[np]>0)   {       if(mark[np]%2==1)        cifang*=quan;        mark[np]/=2;        quan*=quan;             }      sum+=cifang*p*huo[np];     }      else{                           for(int i=0;i<tutu[np].size();i++)            {                  mark[tutu[np][i]]=mark[np]+1;           duilie.push(tutu[np][i]);         }         }   }    //cout<<fixed<<setprecision(1)<<sum;       printf("%.1lf",sum);return 0;}



原创粉丝点击