1090. Highest Price in Supply Chain (25)<BFS,DFS>

来源:互联网 发布:photoshop cs软件下载 编辑:程序博客网 时间:2024/06/08 07:24

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.001 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

深搜广搜都行

这题连题目都没看,直接看的输入,然后画个图,测试一下答案对的,就照着做了

并查集也能做,就不另附代码了


这两题差不多

1079. Total Sales of Supply Chain (25)<BFS,DFS,并查集>

BFS:

#include<cstdio>#include<cmath>#include<algorithm>#include<iostream>#include<cstring>#include<queue>#include<vector>#include<set>#include<map>#include<stack>using namespace std;const int INF=1<<29;int n;double p,r;vector<int> v[100001];int len[100001]={0};int maxn=0;void BFS(int x){for(int i=0;i<100001;i++) len[i]=INF;queue<int> que;len[x]=0;que.push(x);while(que.size()){int num=que.front();que.pop();for(int i=0;i<v[num].size();i++){if(len[v[num][i]]>len[num]+1){   len[v[num][i]]=len[num]+1; //  cout<<v[num][i]<<" "<<len[v[num][i]]<<endl;   que.push(v[num][i]);   if(len[v[num][i]]>maxn) maxn=len[v[num][i]];}}}}int main(){cin>>n>>p>>r;r=(1+r/100.0);int first=0;for(int i=0;i<n;i++){int num;cin>>num;if(num==-1){first=i;continue;}v[num].push_back(i);}BFS(first);int sum=0;for(int i=0;i<n;i++){if(len[i]==maxn) sum++;}printf("%.2f %d",p*pow(r,maxn),sum);return 0;}

DFS:

#include<cstdio>#include<cmath>#include<algorithm>#include<iostream>#include<cstring>#include<queue>#include<vector>#include<set>#include<map>#include<stack>using namespace std;const int INF=1<<29;int n;double p,r;vector<int> v[100001];int len[100001]={0};int maxn=0;void DFS(int x,int cn){if(v[x].size()==0){len[x]=cn;if(len[x]>maxn) maxn=len[x];return ;}for(int i=0;i<v[x].size();i++) DFS(v[x][i],cn+1); }int main(){cin>>n>>p>>r;r=(1+r/100.0);int first=0;for(int i=0;i<n;i++){int num;cin>>num;if(num==-1){first=i;continue;}v[num].push_back(i);}DFS(first,0);int sum=0;for(int i=0;i<n;i++){if(len[i]==maxn) sum++;}printf("%.2f %d",p*pow(r,maxn),sum);return 0;}



原创粉丝点击