码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-31 20:47:22      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

简单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)

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5546952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!