pat 1053

简单回溯,其实还可以再剪枝的,数据太小,没有超时,也就懒得改了o(╯□╰)o

#include<stdio.h>
#include<vector>
#include<algorithm>
#define SIZE 100
using namespace std;

struct tree{
    int index;
    int weight;
    bool flag;
} t[SIZE];

vector <int> node[SIZE];
vector<int>p;

bool cmp(const int &a, const int &b){
    return t[a].weight > t[b].weight;
}

void FindPath(int s, int weight,int sum){
    sum += t[s].weight;
    t[s].flag = true;
    p.push_back(t[s].weight);
    int i;
    if (sum == weight&&!node[s].size()){
        for (i = 0; i < p.size(); i++){
            printf("%d",p[i]);
            if (i == p.size() - 1)
                putchar('\n');
            else putchar(' ');
        }
        p.pop_back();
        return;
    }
    sort(node[s].begin(), node[s].end(),cmp);
    for (i = 0; i < node[s].size(); i++)
        FindPath(node[s][i], weight, sum);
    p.pop_back();
}

int main(){
    freopen("1.in", "r", stdin);
    int noneleaf, nodes, weight;
    scanf("%d%d%d", &nodes, &noneleaf, &weight);
    int i;
    for (i = 0; i < nodes; i++){
        scanf("%d", &t[i].weight);
        t[i].flag = false;
        t[i].index = i;
    }
    int index, j, numofkids, kids;
    for (i = 0; i < noneleaf; i++){
        scanf("%d%d", &index, &numofkids);
        for (j = 0; j < numofkids; j++){
            scanf("%d", &kids);
            node[index].push_back(kids);
        }
    }
    FindPath(0, weight,0);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值