zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    如何向线程传递参数
    IntelliJ IDEA 13 Keygen
    单链表的基本操作
    顺序表静态查找
    有向图的十字链表表存储表示
    BF-KMP 算法
    图的邻接表存储表示(C)
    二叉树的基本操作(C)
    VC远控(三)磁盘显示
    Android 数独游戏 记录
  • 原文地址:https://www.cnblogs.com/shuibeng/p/13640215.html
Copyright © 2011-2022 走看看