PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
简单dfs
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000+10; vector<int>g[maxn]; int n; double P,r; int ans_count=0; double ans_price=99999999; void dfs(int x,double price) { if(g[x].size()==0) { if(price<ans_price) { ans_count=1; ans_price=price; } else if(price==ans_price) ans_count++; return; } for(int i=0;i<g[x].size();i++) { dfs(g[x][i],price*(1+r/100)); } } int main() { scanf("%d",&n); scanf("%lf%lf",&P,&r); for(int i=0;i<n;i++) { int num; scanf("%d",&num); while(num--) { int x; scanf("%d",&x); g[i].push_back(x); } } dfs(0,P); printf("%.4lf %d\n",ans_price,ans_count); return 0; }