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

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
vector<int>Tree[maxn];
long long val[maxn];
long long path[maxn];
int n,m;
long long W;

bool cmp(const int &a,const int &b)
{
    return val[a]>val[b];
}

void read()
{
    scanf("%d%d%lld",&n,&m,&W);
    for(int i=0;i<n;i++) scanf("%lld",&val[i]);
    for(int i=1;i<=m;i++)
    {
        int id; scanf("%d",&id);
        int k; scanf("%d",&k);
        while(k--)
        {
            int to; scanf("%d",&to);
            Tree[id].push_back(to);
        }
    }
}

void dfs(long long sum,int x,int deep)
{
    if(sum>W) return;
    if(sum==W)
    {
        if(Tree[x].size()==0){
            printf("%lld",val[0]);
            for(int i=0;i<deep;i++)
                printf(" %lld",path[i]);
            printf("\n");
        }
        return;
    }

    for(int i=0;i<Tree[x].size();i++)
    {
        path[deep]=val[Tree[x][i]];
        dfs(sum+val[Tree[x][i]],Tree[x][i],deep+1);
    }
}

void work()
{
    for(int i=0;i<n;i++)
        sort(Tree[i].begin(),Tree[i].end(),cmp);
    dfs(val[0],0,0);
}

int main()
{
    read();
    work();
    return 0;
}

 

以上是关于PAT (Advanced Level) 1053. Path of Equal Weight (30)的主要内容,如果未能解决你的问题,请参考以下文章

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

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095