backtracking,
使用binary tree存储,设置child和sibling指针,child=-1表示叶子节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <cstdio> int n; double p,r; struct{ int child,sibling; }t[100001]; int min=100001; int num=0; //the number of retailers that sell at the lowest price void dfs(int p,int ct){ if(ct>min) return; //cut branches if(p<0){ if(ct<min){ min=ct; num=1; } else num++; return; } dfs(t[p].child,ct+1); if(t[p].sibling>0) dfs(t[p].sibling,ct); } int main(){ scanf("%d%lf%lf",&n,&p,&r); for(int i=0;i<n;i++) t[i].child=t[i].sibling=-1; for(int i=0;i<n;i++){ int m,p; scanf("%d",&m); for(int j=0;j<m;j++){ int ch; scanf("%d",&ch); if(t[i].child<0) t[i].child=ch; else t[p].sibling=ch; p=ch; } } dfs(0,0); for(int i=0;i<min-1;i++) p*=1.0+r/100.0; printf("%.4lf %d",p,num); return 0; } |