排序很关键
#include<bits/stdc++.h>
using namespace std;
int N,M,S,id,k,num;
const int maxn = 110;
int path[maxn];
struct node{
int wegiht;
vector<int> child;
}Node[maxn]; //结点数组
bool cmp(int a,int b){
return Node[a].wegiht > Node[b].wegiht;
}
void DFS(int index,int h, int sum){
if(sum > S) return;
else if(sum==S){
if(Node[index].child.size() != 0) return;
for(int i=0;i<h;i++){
if(i!=0) printf(" ");
printf("%d",path[i]);
}
printf("\n");
return;
}else{
for(int i=0;i<Node[index].child.size();i++){
int tmp = Node[index].child[i];
path[h] = Node[tmp].wegiht;
DFS(Node[index].child[i],h+1,sum+Node[tmp].wegiht);
}
return;
}
}
int main(){
scanf("%d%d%d",&N,&M,&S);
for(int i=0;i<N;i++){
scanf("%d",&Node[i].wegiht);
}
for(int i=0;i<M;i++){
scanf("%d%d",&id,&k);
for(int j=0;j<k;j++){
scanf("%d",&num);
Node[id].child.push_back(num);
}
sort(Node[id].child.begin(),Node[id].child.end(),cmp);
}
path[0]=Node[0].wegiht;
DFS(0,1,Node[0].wegiht);
return 0;
}