zoukankan      html  css  js  c++  java
  • PAT甲级 Highest Price in Supply Chain (BFS)

    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.

    输入描述:

    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 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 Si is the index of the supplier for the i-th member.  Sroot for the root supplier is defined to be -1.  All the numbers in a line are separated by a space.



    输出描述:

    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 1010.

    输入例子:

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

    输出例子:

    1.85 2

    思路:这个题和乙级里面的小字辈很像 经典bfs
       注意java里面double保留几位小数的格式
     1 import java.math.BigDecimal;
     2 import java.util.ArrayDeque;
     3 import java.util.ArrayList;
     4 import java.util.Scanner;
     5 
     6 public class Main {
     7     static int n,parent=0;
     8     static double p,r;
     9     static int maxn =100010;
    10     static ArrayList<Integer> v[] = new ArrayList[100010];
    11     static ArrayList<Integer> f[] = new ArrayList[100010];
    12     static int high = 0;
    13     static ArrayDeque<Integer> q = new ArrayDeque<Integer>();
    14     static void bfs() {
    15         q.offer(parent);
    16         
    17         f[high].add(parent);
    18         while(!q.isEmpty()) {
    19             int sum = q.size();
    20             high++;
    21             for(int i=0;i<sum;i++) {
    22                 int head = q.poll();
    23                 for(int j=0;j<v[head].size();j++) {
    24                     f[high].add(v[head].get(j));
    25                     q.offer(v[head].get(j));
    26                 }
    27             }
    28         }
    29     }
    30     public static void main(String[] args) {
    31         Scanner cin = new Scanner(System.in);
    32         for(int i=0;i<maxn;i++) {
    33             v[i] = new ArrayList<Integer>();
    34             f[i] = new ArrayList<Integer>();
    35         }
    36         n = cin.nextInt();
    37         p = cin.nextDouble();r = cin.nextDouble();
    38         for(int i=0;i<n;i++) {
    39             int x = cin.nextInt();
    40             if(x!=-1) {
    41                 v[x].add(i);
    42             }
    43             else {
    44                 parent = i;
    45             }
    46         }
    47         bfs();
    48         double ans = p*Math.pow((1+r*0.01), high-1);
    49         System.out.println(String.format("%.2f", ans)+" "+f[high-1].size());
    50         
    51     }        
    52 }
  • 相关阅读:
    IO模型
    协程
    线程
    进程总结
    HashMap和Hashtable有什么区别
    HashMap 1.7 与 1.8 的 区别,说明 1.8 做了哪些优化,如何优化的
    GC线程是否为守护线程?
    float f=3.4;是否正确?
    final、finally和finalized的区别?
    Eureka注册中心是什么?
  • 原文地址:https://www.cnblogs.com/1013star/p/10356281.html
Copyright © 2011-2022 走看看