I am not serious about reading the questions. I am entangled for a long time: Each path occupies a line with printed weights from the root to the leaf in order. in fact, the final output is sorted by the path (int) stack [n] is saved to (char) S [I], and S [I] is sorted by string to obtain the output results that meet the requirements. C language source code: [cpp] # include <stdio. h> # include <stdlib. h> # include <string. h> typedef struct child {int num; int w;} child; typedef struct node {int w; int top; child c [200];} node; node A [200]; int cmp (const void * a, const void * B) {child * aa = (child *) a; child * bb = (child *) B; return bb-> w-aa-> w;} int cmp1 (const void * a, const void * B) {return strcmp (char *) B, (char *) a);} int Stack [200], top, sum, s; char S [200] [200]; int tops [200], nu; void dfs (int k) {int I; sum + = A [k]. w; Stack [top ++] = A [k]. w; if (A [k]. top = 0) {if (sum = s) {tops [nu] = top; for (I = 0; I <top; I ++) S [nu] [I] = Stack [I]; S [nu] [I] = '\ 0'; nu ++ ;}} else for (I = 0; I <A [k]. top; I ++) dfs (A [k]. c [I]. num); sum-= A [k]. w; top --;} int main () {int n, m, I, num, j, x; scanf ("% d", & n, & m, & s); for (I = 0; I <200; I ++) {A [I]. top = 0; tops [I] = 0 ;}for (I = 0; I <n; I ++) scanf ("% d", & A [I]. w); for (I = 0; I <m; I ++) {scanf ("% d", & num); scanf ("% d ", & A [num]. top); for (j = 0; j <A [num]. top; j ++) {scanf ("% d", & x); A [num]. c [j]. num = x; A [num]. c [j]. w = A [x]. w;}/* qsort (A [num]. c, A [num]. top, sizeof (A [num]. c [0]), cmp); */} www.2cto.com top = 0; sum = 0; nu = 0; dfs (0); qsort (S, nu, sizeof (S [0]), cmp1); for (I = 0; I <nu; I ++) {for (j = 0; j <(int) strlen (S [I])-1; j ++) printf ("% d", S [I] [j]); printf ("% d \ n ", S [I] [j]);} return 0 ;}