#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 = ) : weight(w){}
}; int str2num(const char* str) {
if (str == NULL) return ;
int v = ;
int i = ;
char ch;
while ((ch = str[i++]) != '\0') {
v = v * + (ch - '');
}
return v;
} void print_nodes(vector<Node> &nodes) {
for (int i=; i<nodes.size(); i++) {
printf("id:%d nchild:%d\n", i, nodes[i].child.size());
for (int j=; 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=; 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 = ;
if (a->size() > b->size()) {
len = b->size();
} else {
len = a->size();
}
for (int i=; i<len; i++) {
if ((*a)[i] > (*b)[i]) return true;
}
return false;
} void print_path() {
int len = paths.size();
for (int i=; i<len; i++) {
int plen = paths[i]->size();
printf("%d", (*paths[i])[]);
for (int j=; 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[]; for (int i=; i<N; i++) {
int w = ;
scanf("%d", &w);
nodes[i].weight = w;
}
cmpcls cmpobj(&nodes); for (int i=; i<M; i++) {
int nchild = ;
scanf("%s%d", buf, &nchild);
Node& node = nodes[str2num(buf)];
for (int j=; 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, , , S);
print_path();
return ;
}

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

最新文章

  1. Qt下的udp编程
  2. ArcGIS制图之Subset工具点抽稀
  3. Swift语言
  4. nrf51822-主从通信分析1
  5. 1010. Radix (25)
  6. 【WPF】Dispatcher及线程操作
  7. oracle误删的表恢复
  8. 香蕉派路由功Openwrt、Android功耗对照測试
  9. php 语言特点
  10. JF厂V8版本爱彼AP15703,黄家橡树离岸型,超越N厂神器
  11. angular 4 router传递数据三种方法
  12. vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2) 但是 2:7.3.429-2ubuntu2.1 正要被安装
  13. spring Mongodb查询索引报错 java.lang.NumberFormatException: empty String
  14. ubuntu18.04搭建nfs
  15. 【Spark调优】大表join大表,少数key导致数据倾斜解决方案
  16. P2123 皇后游戏
  17. 用virtualenv建立独立虚拟环境 批量导入模块信息
  18. 使用express框架和mongoose在MongoDB新增数据
  19. 踏破铁鞋无觅处,从AsyncTask学Android线程池
  20. 源码分析篇 - Android绘制流程(一)窗口启动流程分析

热门文章

  1. J2EE 的体系结构
  2. SDUT OJ 效率至上(线段树)
  3. 2、开始学习C++
  4. 如何在JAVA中每隔一段时间执行一段程序
  5. LeetCode记录之35——Search Insert Position
  6. MySQL 安装 linux ,window 傻瓜版
  7. FJOI2019划水记
  8. 链表 206 Reverse Linked List, 92,86, 328, 2, 445
  9. 上海高校程序设计竞赛 D CSL 的字符串 ( 贪心)
  10. 红米note_维修_开机键