球 Debug
查看原帖
球 Debug
757040
MuLinnnnn楼主2024/4/1 21:01
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
int a[23];
bool edge[23][23];
int path[23];
bool vis[23];
int ansPath[23];
int Ans = 0, cnt = 0;

inline bool check(int pos){
	for(int i = 1; i <= n; ++i){
		if(edge[pos][i] && !vis[i]){
			return false; // Yes dig
		}
	}
	return true; // No dig
}

inline void dfs(int pos, int step, int sum){
	if(check(pos)){
		if(Ans < sum){
			Ans = sum;
			cnt = step;
			for(int i = 1; i <= step; ++i){
				ansPath[i] = path[i];
			}
		}
		return ;
	}
	for(int i = 1; i <= n; ++i){
		if(edge[pos][i] && !vis[i]){
			vis[i] = true;
			path[step + 1] = i;
			dfs(i, step + 1, sum + a[i]);
			vis[i] = false;
		}
	}
}

int main(){
	int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; ++i){
    	scanf("%d",&a[i]);
	}
    for(int i = 1; i < n; ++i){
    	for(int j = i + 1; j <= n; ++j){
    		scanf("%d",&edge[i][j]);
		}
	}
	for(int i = 1; i <= n; ++i){
		vis[i] = true;
		path[1] = i;
		dfs(i, 1, a[i]);
		vis[i] = false;
	}
	for(int i = 1; i <= cnt; ++i){
		printf("%d ",ansPath[i]);
	}
	printf("\n%d",Ans);
	return 0;
}

感谢!

2024/4/1 21:01
加载中...