当前位置: 代码迷 >> 综合 >> PAT_A 1106. Lowest Price in Supply Chain (25)
  详细解决方案

PAT_A 1106. Lowest Price in Supply Chain (25)

热度:81   发布时间:2024-01-11 13:45:31.0

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 (<=105), 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:Ki ID[1] ID[2] ... ID[Ki]where in the i-th line, Ki 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. Kj 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 1010.
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
0Sample Output:1.8362 2
  • 简述
    最终所求是一棵树的深度问题。题目很简单,但自己一直有一个检测点运行超时,想了很久。最终通过备忘录的方式解决,之前知道备忘录,但终于用上并深刻体会到了。以下有种方式会很耗时即 1->2->3->4 4->5 4->6 4->7 4->8 。如果直接用循环或者递归的计算深度的话,根据题目线性最多100000,就会变成100000*100000/4,从 O(n)变成了O(n2),再加上题目运行时间限制,很容易超时。
    教训 运行超时,建立备忘录(在设计时,就建立吧,如果内存够用)。
  • 代码一(AC)
#include<iostream>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std;
int p[100005];
int d[100005];
vector<int> r;
int count=1;
int n; 
int deep(int site)
{if(site==0)return 0;int pa=p[site];//就差这一步,备忘录,减少运算//正是由于备忘录的存在,避免了重复计算,// 1->2->3->4 4->5 4->6 4->7 4->8//1-2-3-4这部分有原来每次都计算,现在只计算一遍,从O(n2)-->O(n)if(d[pa]==-1)return d[site]=deep(pa)+1;return d[site]=d[pa]+1;
}
int main()
{double price=0;double rate=0;memset(p,-1,sizeof(p));memset(d,-1,sizeof(d));cin>>n>>price>>rate;for(int i=0;i<n;i++){int tmp=0;cin>>tmp;if(tmp==0){r.push_back(i);}for(int j=0;j<tmp;j++){int rs=0;cin>>rs;p[rs]=i;}}int min=100005;for(int i=0;i<r.size();i++){int tmp=deep(r[i]);if(min>tmp){min=tmp;count=1;}else if(min==tmp)count++;}double minp=price*pow(1+rate/100,min);printf("%.4f %d\n",minp,count);return 0;
}
  • 代码二(递归比循环省地方)
//循环计算深度和最小数量的函数,挺长的
int getdeep()
{int size=r.size();//int min=size+1;这是错误的int min=100005;for(int i=0;i<size;i++){int tmp=r[i];int deep=0;//这费时了while(p[tmp]!=-1){deep++;tmp=p[tmp];}if(min>deep){min=deep;count=1;}else if(min==deep){count++;}}return min;
}
  • 代码三(用链表做,我自己疯了)
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int n; 
struct tree
{bool leave;tree*p;tree(){p=NULL;leave=false;}
};
int count=1;
int lowest(vector<tree*>&t,vector<int>&v)
{int size=v.size();int min=size;for(int i=0;i<size;i++){tree*tmp=t[i];if(tmp->leave==true){//计算量跟代码二的差不多,同样是多重计算树干,超时//但从代码量、简洁程度、安全程度、理解程度用数组的方式更好,建立备忘录也容易。这都是教训~~while(tmp->p!=NULL){v[i]++;tmp=tmp->p;}if(min>v[i]){min=v[i];count=1;} else if(min==v[i]){count++;}}}return min;
}
int main()
{int n=0;double price=0;double rate=0;vector<tree*>t;vector<int>v;cin>>n>>price>>rate;for(int i=0;i<n;i++){tree *tmp=new tree;t.push_back(tmp);v.push_back(0);}for(int i=0;i<n;i++){int tmp=0;int tmp2=0;cin>>tmp;tree*ttmp=t[i];if(tmp==0){t[i]->leave=true;continue;}for(int j=0;j<tmp;j++){int pos;cin>>pos;tree*tt=t[pos];tt->p=ttmp;}}int min=lowest(t,v);double minp=price*pow(1+rate/100,min);printf("%.4f ",minp);cout<<count<<endl;return 0;
}
还有,输出格式话使用的是c语言形势的,以后还是专门记以下iomap吧,不然有点不伦不类。
  相关解决方案