1053 Path of Equal Weight (30 分)
思路
PAT还是很严谨的,有个sort的id写成了i,PAT一直有个测试点没过,但牛客网可以过,太坑了。。
这里有个sort,对结构体的vector进行排序,非常妙,用到了node里面的权重值对vector排序
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
struct node{
int weight;
vector<int>child;
}Node[maxn];
int N,M,K;
int path[maxn]={0};
int num;
void DFS(int index,int weight_sum,int depth)
{
weight_sum = weight_sum+Node[index].weight;
path[depth] = Node[index].weight;
if(Node[index].child.size()==0)
{
if(weight_sum == K)
{
for(int i=0;i<depth;i++)
{
cout<<path[i]<<" ";
}
cout<<path[depth]<<endl;
}
}
else
{
for(int j=0;j<Node[index].child.size();j++)
{
DFS(Node[index].child[j],weight_sum,depth+1);
}
}
}
bool cmp(int a,int b)
{
return Node[a].weight>Node[b].weight;
}
int main()
{
int id,number,ch;
cin>>N>>M>>K;
for(int i =0;i<N;i++)
{
cin>>number;
Node[i].weight = number;
}
for(int i =0;i<M;i++)
{
cin>>id>>number;
for(int j=0;j<number;j++)
{
cin>>ch;
Node[id].child.push_back(ch);
}
sort(Node[id].child.begin(),Node[id].child.end(),cmp);
}
DFS(0,0,0);
}