PAT甲级 A1053
解题思路
理论上这道题用深搜加上排序输出即可实现,但是在测试过程中,测试点3一直过不去,初始代码为
代码:
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<memory.h>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
#include<stack>
using namespace std;
class Node {
public:
int weight;
vector<int> sons;
};
vector<Node> tree(101);
class wayweight {
public:
int length=0;
vector<int> wnode;
};
vector<wayweight> wys;
vector<int> savenode;
int N, M, K;
int currentno = 0;
void DFS(int node){
if (tree[node].sons.size() == 0) {
if (currentno == K) {
wayweight temp;
temp.wnode = savenode;
wys.push_back(temp);
return;
}
else {
return;
}
}
for (int i = 0; i < tree[node].sons.size(); i++) {
int sonnos = tree[node].sons[i];
currentno += tree[sonnos].weight;
if (currentno > K) {
currentno -= tree[sonnos].weight;
continue;
}
else {
savenode.push_back(tree[sonnos].weight);
DFS(sonnos);
savenode.pop_back();
currentno -= tree[sonnos].weight;
}
}
}
bool cmp(wayweight a, wayweight b) {
if (a.wnode.size() == b.wnode.size()) {
for (int i = 0; i < a.wnode.size(); i++) {
if (a.wnode[i] < b.wnode[i]) {
return false;
}
}
return true;
}
else if (a.wnode.size() < b.wnode.size()) {
for (int i = 0; i < a.wnode.size(); i++) {
if (a.wnode[i] < b.wnode[i]) {
return false;
}
}
return true;
}
else {
for (int i = 0; i < b.wnode.size(); i++) {
if (a.wnode[i] < b.wnode[i]) {
return false;
}
}
return true;
}
}
int main() {
cin >> N >> M >> K;
for (int i = 0; i < N; i++) {
cin >> tree[i].weight;
}
for (int i = 0; i < M; i++) {
int nos; cin >> nos;
int sonno; cin >> sonno;
for (int j = 0; j < sonno; j++) {
int son; scanf_s("%d", &son);
tree[nos].sons.push_back(son);
}
}
currentno = tree[0].weight;
savenode.push_back(tree[0].weight);
DFS(0);
for (int i = 0; i < wys.size(); i++) {
for (int j = i + 1; j < wys.size(); j++) {
if (cmp(wys[i], wys[j]) == 0) {
wayweight temp;
temp = wys[i];
wys[i] = wys[j];
wys[j] = temp;
}
}
}
for (int i = 0; i < wys.size(); i++) {
for (int j = 0; j < wys[i].wnode.size(); j++) {
if (j == wys[i].wnode.size() - 1) {
cout << wys[i].wnode[j] << '\n';
break;
}
cout << wys[i].wnode[j] << " ";
}
}
}
补充:经过不懈的努力找到了测试点3无法过去的原因,不要对所有的路径进行排序,将所有的节点子节点按照大小进行排序,即可顺利通过测试点3
以下为AC代码
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<memory.h>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
#include<stack>
using namespace std;
class Node {
public:
int weight;
vector<int> sons;
};
vector<Node> tree(101);
class wayweight {
public:
int length=0;
vector<int> wnode;
};
vector<wayweight> wys;
vector<int> savenode;
int N, M, K;
int currentno = 0;
void DFS(int node){
if (tree[node].sons.size() == 0) {
if (currentno == K) {
wayweight temp;
temp.wnode = savenode;
wys.push_back(temp);
return;
}
else {
return;
}
}
for (int i = 0; i < tree[node].sons.size(); i++) {
int sonnos = tree[node].sons[i];
currentno += tree[sonnos].weight;
if (currentno > K) {
currentno -= tree[sonnos].weight;
continue;
}
else {
savenode.push_back(tree[sonnos].weight);
DFS(sonnos);
savenode.pop_back();
currentno -= tree[sonnos].weight;
}
}
}
bool cmp2(int a, int b) {
return tree[a].weight>tree[b].weight;
}
int main() {
cin >> N >> M >> K;
for (int i = 0; i < N; i++) {
cin >> tree[i].weight;
}
for (int i = 0; i < M; i++) {
int nos; cin >> nos;
int sonno; cin >> sonno;
for (int j = 0; j < sonno; j++) {
int son; scanf_s("%d", &son);
tree[nos].sons.push_back(son);
}
}
for (int i = 0; i < N; i++) {
sort(tree[i].sons.begin(), tree[i].sons.end(), cmp2);
}
currentno = tree[0].weight;
savenode.push_back(tree[0].weight);
DFS(0);
for (int i = 0; i < wys.size(); i++) {
for (int j = 0; j < wys[i].wnode.size(); j++) {
if (j == wys[i].wnode.size() - 1) {
cout << wys[i].wnode[j] << '\n';
break;
}
cout << wys[i].wnode[j] << " ";
}
}
}