开始犯蠢想用set实现从小到大,写到一般才发现输出的是权值从大到小。
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 110;
int n, m, s;
int path[maxn];//定义一个int型的path,实际是一个不断覆盖的过程
struct Node
{
int w;
vector<int> v;
}node[maxn];
bool cmp(int a, int b)
{
return node[a].w > node[b].w;
}
void dfs(int root, int depth, int ans)//ans 初值取root的weight
{
if (ans > s) return;
else if (ans == s)
{
if (node[root].v.size() != 0) return;
for (int i = 0; i < depth; i++)
{
printf("%d", node[path[i]].w);
if (i != depth - 1) printf(" ");
}
printf("\n");
return;
}
for (int i = 0; i < node[root].v.size(); i++)
{
int child = node[root].v[i];
path[depth] = child;
dfs(child, depth + 1, ans + node[child].w);
}
}
int main()
{
int id, idnum, temp;
scanf("%d%d%d", &n, &m, &s);
for (int i = 0; i < n; i++)
{
scanf("%d", &node[i].w);
}
for (int i = 0; i < m; i++)
{
scanf("%d%d", &id, &idnum);
for (int j = 0; j < idnum; j++)
{
scanf("%d", &temp);
node[id].v.push_back(temp);
}
sort(node[id].v.begin(), node[id].v.end(), cmp);
}
path[0] = 0;
dfs(0, 1, node[0].w);
return 0;
}