PAT甲級1090 Highest Price in Supply Chain (25分) 雙vector來記錄圖 隊列實現廣搜

1090 Highest Price in Supply Chain (25分)

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. 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 highest price we 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 (≤10​5​​), the total number of the members in the supply chain (and hence they are numbered from 0 to N−1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number S​i​​ is the index of the supplier for the i-th member. S​root​​ for the root supplier is defined to be −1. All the numbers in a line are separated by a space.
Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 10​10​​.
Sample Input:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:

1.85 2

題目其實就是給定了個單向圖,從根節點開始,找最深的層次。我用的廣搜實現的,但是因爲這裏N也不小,10^5 ,所以存儲的時候使用了Vector<vector< int >> vec(N) 每個結點一個vector< int > 來存儲從該節點出去的邊,不用數組是我感覺會超時,不如雙vector嵌套來記錄這個單向圖
然後一個廣搜搞定,然後有個測試點考察的就是根節點。
大概就是下面我的靈魂配圖
在這裏插入圖片描述
一個結點有多個供應商(父節點,也無所謂,後面再次訪問到了這個結點就更改下,因爲在廣搜裏面,後面訪問到的一定層次是更深的)

#include <iostream>
#include <string>
using namespace std;
#include<vector>
#include<algorithm>
#include<queue>
int sum(int n)//中序遍歷
{
    int out=0;
    while(n!=0)
    {
        out+=n%10;
        n=n/10;
    }
    return out;
}

int main()
{
    int N;
    double P;
    double r;
    cin>>N>>P>>r;
    vector<vector<int>> vec(N);
    int root;
    for(int i=0; i<N; i++)
    {
        int num=i;//結點編號
        int sup;
        cin>>sup;//供應商
        if(sup!=-1)
        {
            vec[sup].push_back(num);
        }
        else
            root=num;
    }
    int level_num;

    queue<int> que;
    que.push(root);
    int number=1;//有個測試點考察的就是最高價格就是根節點的價格,輸出就是1
    while(!que.empty())
    {
        queue<int> get;
        while(!que.empty())
        {
            vector<int> number=vec[que.front()];
            int start=que.front();
            for(int i=0; i<number.size(); i++)
            {

                int endd=number[i];
                get.push(number[i]);
            }
            que.pop();

        }
        if(get.size()!=0){ //這裏是因爲當進行到最後一個層次的時候,que的隊列全部出棧,get爲空
            que=get;
            number=get.size();
            P=P*r*0.01+P;
        }
    }
printf("%.2f %d",P,number);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章