A1053 Path of Equal Weight (30 分)

开始犯蠢想用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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值