PAT甲级真题 1090 Highest Price in Supply Chain (25分) C++实现(dfs)

题目

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 (≤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 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

思路

用邻接表建图,将标记为-1的根节点记录到第n个位置。

从根节点DFS遍历,用局部变量depth记录深度;达到叶子时比较深度是否最大,找到最大深度和最大深度叶子的个数,记录到maxDepth、num中。

需要注意的是:

  1. p和r需要用double存放,用float测试点1、2、6都会报错;
  2. 从根节点拿到的商品价格为p,无需涨价(这点纠结了好久…)

代码

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

int maxDepth = 0;
int num = 0;

void dfs(vector<vector<int> > &e, int cur, int depth){
    if (e[cur].size()==0){
        if (depth > maxDepth){
            maxDepth = depth;
            num = 1;
        }
        else if (depth == maxDepth){
            num++;
        }
        return;
    }
    depth++;    
    for (int i=0; i<e[cur].size(); i++){
        dfs(e, e[cur][i], depth);
    }
}

int main() { 
    int n;
    double p, r;
    scanf("%d%lf%lf", &n, &p, &r);
    vector<vector<int> > e(n+1);  //邻接表,最后一个存储root
    for (int i=0; i<n; i++){
        int src;
        scanf("%d", &src);
        if (src==-1){
            e[n].push_back(i);
        }
        else{
            e[src].push_back(i);
        }
    }
    dfs(e, n, 0);
    printf("%.2f %d\n", pow((1+r/100.0), maxDepth-1) * p, num);
    return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是高响应比优先调度算法C语言实现的示例代码: ``` #include <stdio.h> #define MAX_PROCESS 10 typedef struct { int pid; int arrival_time; int burst_time; int waiting_time; int turnaround_time; float priority; int completed; } Process; void sort(Process process[], int n) { // Sort the processes by priority in descending order for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (process[i].priority < process[j].priority) { Process temp = process[i]; process[i] = process[j]; process[j] = temp; } } } } int main() { int n, total_waiting_time = 0, total_turnaround_time = 0; printf("Enter the number of processes: "); scanf("%d", &n); Process process[MAX_PROCESS]; // Input the process data for (int i = 0; i < n; i++) { printf("Enter the arrival time and burst time of process %d: ", i + 1); scanf("%d%d", &process[i].arrival_time, &process[i].burst_time); process[i].pid = i + 1; process[i].completed = 0; } // Calculate the priority of each process for (int i = 0; i < n; i++) { process[i].priority = 1 + (float)(process[i].waiting_time) / process[i].burst_time; } int current_time = 0, completed = 0; printf("\nGantt Chart:\n"); while (completed != n) { int highest_priority_index = -1; float highest_priority = -1; for (int i = 0; i < n; i++) { if (process[i].arrival_time <= current_time && !process[i].completed) { if (process[i].priority > highest_priority) { highest_priority = process[i].priority; highest_priority_index = i; } } } if (highest_priority_index == -1) { // No process available at the moment printf("idle "); current_time++; continue; } printf("P%d ", process[highest_priority_index].pid); process[highest_priority_index].waiting_time = current_time - process[highest_priority_index].arrival_time; current_time += process[highest_priority_index].burst_time; process[highest_priority_index].turnaround_time = current_time - process[highest_priority_index].arrival_time; total_waiting_time += process[highest_priority_index].waiting_time; total_turnaround_time += process[highest_priority_index].turnaround_time; process[highest_priority_index].completed = 1; completed++; sort(process, n); // Sort the processes again by priority } // Print the process table printf("\n\nProcess Table:\n"); printf("--------------------------------------------------\n"); printf("| Process | Arrival Time | Burst Time | Waiting Time | Turnaround Time |\n"); printf("--------------------------------------------------\n"); for (int i = 0; i < n; i++) { printf("| P%-6d | %-12d | %-10d | %-12d | %-15d |\n", process[i].pid, process[i].arrival_time, process[i].burst_time, process[i].waiting_time, process[i].turnaround_time); } printf("--------------------------------------------------\n"); // Print the average waiting time and turnaround time printf("\nAverage Waiting Time: %f", (float)total_waiting_time / n); printf("\nAverage Turnaround Time: %f", (float)total_turnaround_time / n); return 0; } ``` 该算法的主要思想是根据每个进程的等待时间和执行时间计算优先级,然后按照优先级降序排列进程,选择优先级最高的进程执行,执行完毕后重新计算每个进程的优先级并重新排序,直到所有进程都执行完毕。在实现中,使用了一个结构体来存储每个进程的数据,包括进程ID,到达时间,执行时间,等待时间,周转时间和优先级,以及一个标志位表示进程是否已完成。在计算优先级时,使用了一个公式:优先级 = 1 + 等待时间 / 执行时间。在选择进程执行时,如果有多个进程的优先级相同,则选择到达时间早的进程。最后,根据进程的数据和执行结果,输出进程表和平均等待时间、平均周转时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值