题目链接
这道题中规中矩, 但是在结果的输出上面有点小麻烦, 有三种思路。
- 不控制DFS方向, 再DFS结束以后,对所得的结果进行排序, 然后输出,需要用到 vector;
- 在DFS中就控制DFS的方向, 先搜索大的, 这样子在到达叶结点的时候可以直接进行输出, 也可以先保存再输出。
- 和第一个思路类似, 不控制DFS方向, 但是在递归边界处对结果进行排序。需要用到 priority_queue。(不能用set, 因为set会将重复的数据去掉, 但样例中有的路径上面的数据就是完全一样的)
第一种思路的代码如下, 中规中矩, 相对简单,先用 vector 来保存答案, 再对答案排序输出即可。
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct Node {
int weight;
vector<int> child;
}node[110];
bool cmp(vector<int> a, vector<int> b){
for (int i=0; i<min(a.size(), b.size()); i++){
if (a[i]!=b[i]) return node[a[i]].weight>node[b[i]].weight;
}
return true;
}
int s;
vector<vector<int> > ans;
vector<int> tempPath;
void DFS(int root, int weight){
if (node[root].child.size()==0){
tempPath.push_back(root);
if (weight==s){
ans.push_back(tempPath);
}
tempPath.pop_back();
return;
}
if (weight>s){
return;
}
tempPath.push_back(root);
for (int i=0; i<node[root].child.size(); i++){
int child=node[root].child[i];
DFS(child, weight+node[child].weight);
}
tempPath.pop_back();
}
int main(){
int n,m;
scanf("%d %d %d", &n, &m, &s);
for (int i=0; i<n; i++){
scanf("%d",&node[i].weight);
}
for (int i=0; i<m; i++){
int id;
scanf("%d", &id);
int k;
scanf("%d",&k);
for (int j=0; j<k; j++){
int childId;
scanf("%d",&childId);
node[id].child.push_back(childId);
}
}
DFS(0, node[0].weight);
sort(ans.begin(), ans.end(), cmp);
for (int i=0; i<ans.size(); i++){
vector<int> path=ans[i];
for (int j=0; j<path.size(); j++){
if (j) printf(" ");
printf("%d",node[path[j]].weight);
}
printf("\n");
}
}
第二种思路的代码如下,在向下搜索时, 先对孩子进行排序, 先搜索大的,这样子在递归边界处就可以直接输出答案, 当然也可以像我这样, 先保存, 在输出答案。
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct Node {
int weight;
vector<int> child;
}node[110];
bool cmp(int a, int b){
return node[a].weight>node[b].weight;
}
int s;
vector<vector<int> > ans;
vector<int> tempPath;
void DFS(int root, int weight){
if (node[root].child.size()==0){
tempPath.push_back(root);
if (weight==s){
ans.push_back(tempPath);
}
tempPath.pop_back();
return;
}
if (weight>s){
return;
}
tempPath.push_back(root);
sort(node[root].child.begin(), node[root].child.end(), cmp);
for (int i=0; i<node[root].child.size(); i++){
int child=node[root].child[i];
DFS(child, weight+node[child].weight);
}
tempPath.pop_back();
}
int main(){
int n,m;
scanf("%d %d %d", &n, &m, &s);
for (int i=0; i<n; i++){
scanf("%d",&node[i].weight);
}
for (int i=0; i<m; i++){
int id;
scanf("%d", &id);
int k;
scanf("%d",&k);
for (int j=0; j<k; j++){
int childId;
scanf("%d",&childId);
node[id].child.push_back(childId);
}
}
DFS(0, node[0].weight);
for (int i=0; i<ans.size(); i++){
vector<int> path=ans[i];
for (int j=0; j<path.size(); j++){
if (j) printf(" ");
printf("%d",node[path[j]].weight);
}
printf("\n");
}
}
第三种思路如下, 比较麻烦, 作为 set 或者 priority_queue 的排序重载训练倒是挺不错,总体的思路就是, 直接在加入答案的时候自动排序, 然后输出。
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
using namespace std;
struct Node {
int weight;
vector<int> child;
}node[110];
struct Path{
vector<int> vt;
friend bool operator <(Path a, Path b){
for (int i=0; i<min(a.vt.size(), b.vt.size()); i++){
if (a.vt[i]!=b.vt[i]) return node[a.vt[i]].weight<node[b.vt[i]].weight;
}
}
};
int s;
priority_queue<Path > ans;
vector<int> tempPath;
void DFS(int root, int weight){
if (node[root].child.size()==0){
tempPath.push_back(root);
if (weight==s){
ans.push(Path{tempPath});
}
tempPath.pop_back();
return;
}
if (weight>s){
return;
}
tempPath.push_back(root);
for (int i=0; i<node[root].child.size(); i++){
int child=node[root].child[i];
DFS(child, weight+node[child].weight);
}
tempPath.pop_back();
}
int main(){
int n,m;
scanf("%d %d %d", &n, &m, &s);
for (int i=0; i<n; i++){
scanf("%d",&node[i].weight);
}
for (int i=0; i<m; i++){
int id;
scanf("%d", &id);
int k;
scanf("%d",&k);
for (int j=0; j<k; j++){
int childId;
scanf("%d",&childId);
node[id].child.push_back(childId);
}
}
DFS(0, node[0].weight);
while (ans.size()){
for (int i=0; i<ans.top().vt.size(); i++){
if (i) printf(" ");
int id=ans.top().vt[i];
printf("%d",node[id].weight);
}
printf("\n");
ans.pop();
}
}
我还是第一次用priority_queue这个数据结构,他和set的区别就是,priority_queue不会去重,有时候这个性质很有用。