1. 程式人生 > 實用技巧 >PAT A1106 Lowest Price in Supply Chain (25分)

PAT A1106 Lowest Price in Supply Chain (25分)



層序遍歷進行layer的確定

#include<cstdio>
#include<vector>
#include<math.h>
#include<queue>
using namespace std;
const int N = 100010;
const int INF = 100010;
int n;
double p,r;
struct node{
    int layer;
    bool isleaf = true;
    vector<int> child;
}Node[N];
int minlayer = INF;
int count = 0;
void layerOrder(int root){
    queue<int> q;
    q.push(root);
    Node[root].layer = 0;
    while(q.empty()==false){
        int front = q.front();
        q.pop();
        if(Node[front].isleaf==false){
            for(int i = 0;i<Node[front].child.size();i++){
                int id = Node[front].child[i];
                q.push(id);
                Node[id].layer = Node[front].layer+1;
            }
        }else{
            if(Node[front].layer<minlayer){
                minlayer = Node[front].layer;
                count = 1;
            }else if(Node[front].layer==minlayer){
                count++;
            }
        }
    }
    return;
}
int main(){
    scanf("%d %lf %lf",&n,&p,&r);
    for(int i = 0;i<n;i++){//0..n-1
        int childnum;
        scanf("%d",&childnum);
        if(childnum!=0) Node[i].isleaf = false;
        for(int j = 0;j<childnum;j++){
            int childid;
            scanf("%d",&childid);
            Node[i].child.push_back(childid);
        }
    }
    int root = 0;//root預設為0
    layerOrder(root);
    double price = p*pow(1+r/100,minlayer);
    printf("%.4f %d",price,count);
    return 0;
}