--A1053.Path tree traversal of Equal Weight (30) not only DFS BFS

 

 

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int MAXN = 110;
struct Node{
    int weight;
    vector<int> child;
}Node[MAXN];
bool cmp(int a,int b){
    return Node[a].weight > Node[b].weight;
}
int n,m,S;//结点数,边数及给定的和
int path[MAXN];//记录路径
void DFS(int index,int numNode,int sum){
    if(sum > S){
        return;
    }
    if(sum == S){
        if(Node[index].child.size() != 0){
            return;
        }
        for(int i=0;i<numNode;++i){
            printf("%d",Node[path[i]].weight);
            if(i < numNode-1){
                printf(" ");
            }else{
                printf("\n");
            }
        }
        return;
    }
    for(int i=0;i<Node[index].child.size();++i){
        int child = Node[index].child[i];
        path[numNode] = child;
        DFS(child,numNode+1,sum + Node[child].weight);
    }
}
int main(){
    scanf("%d%d%d",&n,&m,&S);
    for(int i=0;i<n;++i){
        scanf("%d",&Node[i].weight);
    }
    int id,k,child;
    for(int i=0;i<m;++i){
        scanf("%d%d",&id,&k);
        for(int j=0;j<k;++j){
            scanf("%d",&child);
            Node[id].child.push_back(child);
        }
        Sort (the Node [ID] .child.begin (), the Node [ID] .child.end (), CMP); 
    } 
    path [ 0 ] = 0 ; // first node set path to node 0 
    the DFS ( 0 , . 1 , the Node [ 0 ] .Weight); 
    System ( " PAUSE " );
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/JasonPeng1/p/12237200.html