1090. Highest Price in Supply Chain (25)(dfs)

本文介绍了一种算法,用于计算给定供应链中零售商能够达到的最高价格及其数量。通过构建图并采用深度优先搜索(DFS),该算法可以高效地找出最长路径及对应的零售商数目。

1090. Highest Price in Supply Chain (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

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

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

一开始直接用循环遍历会超时

根据给定的数组反过来,用vector邻接表建立一个图(树),然后dfs,到底部判断最深的

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int n,num,maxnum;
double p,r;
int pre[100100];
int vis[100100];
vector<int>ans;
vector<int>G[100100];
int root;
bool cmp(int a,int b){
    return a > b;
}
void dfs(int s){
    int i;
    if(!G[s].size()){
        if(num > maxnum){
            maxnum = num;
            ans.clear();
            ans.push_back(num);
        }
        else if(num == maxnum){
            ans.push_back(num);
        }
        return;
    }
    for(i = 0; i < G[s].size(); i++){
        if(!vis[G[s][i]]){
            vis[G[s][i]] = 1;
            num++;
            dfs(G[s][i]);
            vis[G[s][i]] = 0;
            num--;
        }
    }
}
int main(){
    cin >> n >> p >> r;
    r /= 100.0;
    r += 1.0;
    memset(vis,0,sizeof(vis));
    for(int i = 0; i < n; i++){
        cin >> pre[i];
        if(pre[i] == -1) root = i;
    }
    for(int i = 0; i < n; i++){
        if(pre[i] == -1) continue;
        G[pre[i]].push_back(i);
    }
    vis[root] = 1;
    num = 0;
    maxnum = 0;
    dfs(root);
    printf("%.2f %d\n",p*pow(r,ans[0]),ans.size());
    return 0;
}


`Priority = ThreadPriority.Highest` 用于将线程的优先级设置为最高。在多线程编程中,操作系统会根据线程的优先级来分配CPU时间片,优先级高的线程会更优先地获得CPU资源。 ### 含义 此代码将线程的优先级设置为最高级别。在 .NET 框架里,`ThreadPriority` 是一个枚举类型,`ThreadPriority.Highest` 代表最高优先级,与之对应的还有 `Lowest`、`BelowNormal`、`Normal` 和 `AboveNormal` 等优先级[^1][^2][^3]。 ### 使用场景 - **实时任务处理**:在需要实时响应的系统中,如音频或视频处理、游戏开发等,将关键任务的线程优先级设为最高,能确保这些任务优先执行,避免出现延迟。 - **资源竞争环境**:当多个线程竞争同一资源时,把重要线程的优先级设为最高,可保证该线程能优先获取资源,防止数据不一致或冲突。 ### 相关问题及解决方案 - **优先级设置无效**:线程优先级设置可能受操作系统调度策略和系统负载影响,不一定能保证高优先级线程总是先执行。可通过限制进程运行在特定 CPU 内核上,增强优先级设置的效果,如在特定情况下限制一个进程,让线程都在里面跑,并限制住一个 CPU 内核 [^2]。 - **饥饿问题**:若高优先级线程持续占用 CPU,低优先级线程可能长时间无法执行,导致饥饿问题。可通过合理设计线程逻辑,避免高优先级线程长时间占用 CPU,或动态调整线程优先级来解决。 以下是设置线程优先级为最高的示例代码: ```csharp using System; using System.Threading; class Program { static void Main() { Thread t = new Thread(ShowMessage); t.Name = "One"; // 设置线程优先级为最高 t.Priority = ThreadPriority.Highest; t.Start(); Console.ReadKey(); } static void ShowMessage() { for (var i = 0; i < 10; i++) { Thread.Sleep(1000); Console.WriteLine(i); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值