1. 程式人生 > >PAT甲級 1106 Lowest Price in Supply Chain (25 分)dfs

PAT甲級 1106 Lowest Price in Supply Chain (25 分)dfs

1106 Lowest 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. 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 (≤10​5​​), 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:

K​i​​ ID[1] ID[2] ... ID[K​i​​]

where in the i-th line, K​i​​ 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. K​j​​ 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 10​10​​.

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0

Sample Output:

1.8362 2

解析: 大意就是根據輸入能構造出一棵樹,然後求葉子結點(對應零售商)出售標品價值的最小值。用dfs即可輕鬆解決。

注意:(1)res用於存放最低價格,其初始值要設大些,避免出錯。 

           (2)double要用%lf輸入,但要用%f輸出。或者用cin來輸入double,用%f打印出double值

#include<iostream>
#include<cstdio>
#include<vector> 
#include<cmath>
using namespace std;

vector<vector<int>> vec;
int n;
double p,r;

double res = 999999999;//初始值要設大些 
int num = 0;

void dfs(int node,int h){
	if(vec[node].size() == 0){
		double temp = pow(1+r*0.01,h)*p;//r記得換成百分比形式 
		if(temp < res){
			res = temp;
			num = 1;
		}else if(temp == res)
			num++;	
		return;
	}
	for(int i=0;i<vec[node].size();i++)
		dfs(vec[node][i],h+1);
}

int main(){
	scanf("%d %lf %lf",&n,&p,&r);
	vec.resize(n);
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a;
		for(int j=0;j<a;j++){
			cin>>b;
			vec[i].push_back(b);
		}	
	}
	dfs(0,0);
	printf("%.4f %d",res,num);
	
	return 0;
} 

相關推薦

PAT甲級 1106 Lowest Price in Supply Chain 25 dfs

1106 Lowest Price in Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone

PAT 1106 Lowest Price in Supply Chain 25

1106 Lowest Price in Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involve

PAT (Advanced Level) Practice 1106 Lowest Price in Supply Chain 25

#include<cstdio> #include<vector> #include<cmath> using namespace std; const int N=1e5+5; vector<int> G[N]; int num,n;

1106 Lowest Price in Supply Chain 25 樹的遍歷

求葉子結點處能活得最低價格以及能提供最低價格的葉子結點的個數 #include<bits/stdc++.h> using namespace std; const int N=1e5+10; vector<int>vec[N]; int minn=0x3f3f3f3f; in

1106 Lowest 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

PAT 甲級 1106 Lowest Price in Supply Chain

number test https script back urn code name ram https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464 A supply

PAT 1090 Highest Price in Supply Chain 25

1090 Highest Price in Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involv

PAT (Advanced Level) Practice 1090 Highest Price in Supply Chain 25

#include<cstdio> #include<vector> #include<cmath> using namespace std; const int N=1e5+5; vector<int> G[N]; int num,n;

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 suppli

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

1090 Highest Price in Supply Chain 25 dfs

A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involved in moving a product from supplier to customer

PAT 1079 Total Sales of Supply Chain 25

1079 Total Sales of Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involved

PAT (Advanced Level) Practice 1079 Total Sales of Supply Chain 25

#include<cstdio> #include<vector> using namespace std; const int N=1e5+5; int amount[N]; vector<int> G[N]; double p,r,ans;

1079 Total Sales of Supply Chain 25

A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involved in moving a product from suppli

1079 Total Sales of Supply Chain 25

  A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involved in moving a product from supplier to

PAT (Advanced Level) Practice 1036 Boys vs Girls 25 C++甲級

1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of all the male students and the highest gra

PAT甲級真題——1011 World Cup Betting 20

1011 World Cup Betting (20 分) With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the bes

PAT-乙-1060 1060 愛丁頓數 25

程式碼 #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n; cin>>n;

PAT (Advanced Level) Practice 1094 The Largest Generation 25 樹的層次遍歷

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find t

PAT 1043 Is It a Binary Search Tree 25

1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: Th