1106. Lowest Price in Supply Chain (25)

来源:互联网 发布:设有数组定义,x 编辑:程序博客网 时间:2024/05/21 22:49

1106. Lowest Price in Supply Chain (25)

时间限制
200 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 lowest price a customer can expect from some 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 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. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1010.

Sample Input:
10 1.80 1.003 2 3 51 91 41 702 6 11 8000
Sample Output:
1.8362 2
AC代码
#include <iostream>#include <vector>#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;typedef pair<int,int> info;//身份,指数.typedef pair<int,bool> iden;//上层供应商信息,是否初始化.void findExponents(vector<pair<info,iden>> *,int);int main(){    int N,i;    double price,r;    scanf("%d%lf%lf",&N,&price,&r);    vector<pair<info,iden>> total(N,pair<info,iden>(info(0,0),iden(0,false)));//信息,上层供应商ID.    for(i=0;i<N;i++)    {        int k,temp;        scanf("%d",&k);        total[i].first.first=k;//指定身份,为0即为零售商.        for (int j=0; j<k; j++) {            scanf("%d",&temp);            total[temp].second.first=i;//上层供应商ID.        }    }    total[0].second.first=0;    total[0].second.second=true;//初始化根供应商    total[0].first.second=0;//根供应商的指数为0;    for(i=0;i<total.size();i++){        //cout<<i<<endl;        findExponents(&total,i);    }    sort(total.begin(), total.end(),[](pair<info,iden> a,pair<info,iden> b){        if(a.first.first==b.first.first)            return a.first.second<b.first.second;        else            return a.first.first<b.first.first;    });    int Exponents = total[0].first.second;//获取最便宜的指数.    int count=0;    for (pair<info,iden> a : total) {        if(a.first.second==Exponents)            count++;        else            break;    }    for (i=0; i<Exponents; i++)        price*=(1+0.01*r);    printf("%.4lf %d\n",price,count);    return 0;}void findExponents(vector<pair<info,iden>> *all,int index){        if((*all)[index].second.second==false){//未初始化时初始化.        int previousID = (*all)[index].second.first;        if((*all)[previousID].second.second==false)//同样未初始化            findExponents(all, previousID);        (*all)[index].first.second = (*all)[previousID].first.second+1;        (*all)[index].second.second=true;//初始化完毕.    }}

0 0
原创粉丝点击