DFS——小猫爬山

小猫爬山

        dfs暴搜模拟题,按重量从大到小排序,可以在搜索状态中更快获得一组最优解用于剪枝来大大优化效率。

#include
using namespace std;
#define rep(i,a,n) for(int i = a;i=a;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define yes cout<<"YES"<<'\n';
#define no cout<<"NO"<<'\n';
#define endl '\n';
typedef vector VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) {return mrand() % x;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;};
ll lcm(int a,int b){return a*b/gcd(a,b);}

int n,m,ans=INT_MAX;

void dfs(VI &a,VI &p,int k){
	if(k==n||SZ(p)>ans){
		ans=min(ans,SZ(p));
		return;
	}
	rep(j,0,SZ(p)){
		if(p[j]+a[k]<=m&&SZ(p)>n>>m;
	VI a(n);
	rep(i,0,n) cin>>a[i];
	sort(all(a),[&](const int a,const int b){return a>b;});
	vector p;
	dfs(a,p,0);
	cout<

你可能感兴趣的:(DFS,深度优先,算法)