浙大PAT甲级 1053

深度优先搜索。

可使用邻接链表来存储整个图,为了使结果为非增输出,可根据他们的费用从大到小进行排序,然后从0开始进行深度优先搜索。数组path[i]来记录满足条件时,i的后续结点。

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 26*26*26*10+5
using namespace std;
int a[105];
vector v[105];
int n,m,s;
int path[105];
bool cmp(int x,int y)
{
    return a[x]>a[y];
}
void dfs(int x,int cost)
{
    if(cost+a[x]==s)
    {
       if(v[x].empty())
       {
           printf("%d",a[0]);
           for(int i=path[0];i!=-1;i=path[i])
           {
               printf(" %d",a[i]);
           }
           printf("\n");
       }
    }
    else if(cost+a[x]


你可能感兴趣的:(浙大pat)