pat 1053

来源:互联网 发布:通达信敢死队资金源码 编辑:程序博客网 时间:2024/05/21 02:36

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

#include<stdio.h>#include<vector>#include<algorithm>#define SIZE 100using 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;}
0 0