PAT 1053 Path of Equal Weight

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int>* > paths;

class Node {
public:
    vector<int> child;
    int weight;
    Node(int w = 0) : weight(w){}
};

int str2num(const char* str) {
    if (str == NULL) return 0;
    int v = 0;
    int i = 0;
    char ch;
    while ((ch = str[i++]) != ‘\0‘) {
        v = v * 10 + (ch - ‘0‘);
    }
    return v;
}

void print_nodes(vector<Node> &nodes) {
    for (int i=0; i<nodes.size(); i++) {
        printf("id:%d nchild:%d\n", i, nodes[i].child.size());
        for (int j=0; j<nodes[i].child.size(); j++) {
            printf(" %d", nodes[i].child[j]);
        }
        printf("\n");
    }
}

void dfs(vector<Node> &nodes, vector<int> &path, int idx, int sum, int target) {
    if (idx >= nodes.size()) return; // invalid case;
    int path_weight = sum + nodes[idx].weight;
    if (path_weight == target) {
        // not a leaf node, ignore it
        if (!nodes[idx].child.empty()) {
            return;
        }
        // leaf node, so we got a Root->Leaf path weight equal to the target weight
        path.push_back(nodes[idx].weight);
        // record it
        paths.push_back(new vector<int>(path));
        path.pop_back();
        return;
    } else if (path_weight > target) {
        // impossible continue to find a valid path
        return;
    }

    int clen = nodes[idx].child.size();
    for (int i=0; i<clen; i++) {
        path.push_back(nodes[idx].weight);
        dfs(nodes, path, nodes[idx].child[i], path_weight, target);
        path.pop_back();
    }

}
class cmpcls {
private:
    vector<Node>* nodes;
public:
    cmpcls(vector<Node>* ns) : nodes(ns) {}
    bool operator()(int a, int b) {
        if ((*nodes)[a].weight > (*nodes)[b].weight) {
            return true;
        } else {
            return false;
        }
    }
};

bool mycmp(const vector<int>* a, const vector<int>* b) {
    int len = 0;
    if (a->size() > b->size()) {
        len = b->size();
    } else {
        len = a->size();
    }
    for (int i=0; i<len; i++) {
        if ((*a)[i] > (*b)[i]) return true;
    }
    return false;
}

void print_path() {
    int len = paths.size();
    for (int i=0; i<len; i++) {
        int plen = paths[i]->size();
        printf("%d", (*paths[i])[0]);
        for (int j=1; j<plen; j++) {
            printf(" %d", (*paths[i])[j]);
        }
        printf("\n");
    }
}
int main() {
    int N, M, S;

    scanf("%d%d%d", &N, &M, &S);
    vector<Node> nodes(N);

    char buf[4];

    for (int i=0; i<N; i++) {
        int w = 0;
        scanf("%d", &w);
        nodes[i].weight = w;
    }
    cmpcls cmpobj(&nodes);

    for (int i=0; i<M; i++) {
        int nchild = 0;
        scanf("%s%d", buf, &nchild);
        Node& node = nodes[str2num(buf)];
        for (int j=0; j<nchild; j++) {
            scanf("%s", buf);
            node.child.push_back(str2num(buf));
        }
        sort(node.child.begin(), node.child.end(), cmpobj);
    }
    vector<int> path;
    dfs(nodes, path, 0, 0, S);
    print_path();
    return 0;
}

原先写的比较函数有问题一直有个case不对,换个思路直接把childnode的顺序按照weight大小来排,这样最终的结果就是按照规定排序。

时间: 2024-08-29 04:42:18

PAT 1053 Path of Equal Weight的相关文章

PAT 1053. Path of Equal Weight (30)

1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to Lis defined to be the sum of the

PAT Advanced Level 1053 Path of Equal Weight

1053 Path of Equal Weight (30)(30 分) Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf

1053. Path of Equal Weight (30)

dfs函数携带vector形参记录搜索路径 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of

PAT Advanced 1053 Path of Equal Weight (30) [树的遍历]

题目 Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tree,

PAT:1053. Path of Equal Weight (30) AC

#include<stdio.h> #include<vector> #include<queue> #include<algorithm> using namespace std; const int MAX=1010; int n,m; //n个节点,m个非叶子节点 long long int S; //待测权值 long long int weight[MAX]; //每个节点的权值 vector<int> child[MAX]; //存储

PAT (Advanced Level) 1053. Path of Equal Weight (30)

简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; const int maxn=100+10;

PAT甲题题解-1053. Path of Equal Weight (30)-dfs

由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排序,然后再add建边. 水题一个,不多说了. #include <iostream> #include <algorithm> #include <cstdio> #include <string.h> using namespace std; const in

1053 Path of Equal Weight (30分)(并查集)

Given a non-empty tree with root R, and with weight W?i?? assigned to each tree node T?i??. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre

1053 Path of Equal Weight (30 分)(树的遍历)

题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径 #include<bits/stdc++.h> using namespace std; int n,m,sum; const int N=120; struct node { int w; vector<int>p; }tree[N]; bool cmp(int a,int b) { return tree[a].w>tree[b].w; } vector&l