P2196 [NOIP1996 提高组] 挖地雷 dfs
作者:
多米尼克領主的致意
,
2024-05-23 21:08:40
,
所有人可见
,
阅读 6
N <= 20 这题最大常数是n! 正常直接dfs应该会T 数据不够强
直接爆也过了
code:
#include <bits/stdc++.h>
using namespace std;
int w[21];
vector<int>e[21];
int n;
int mx = -0x3f3f3f3f;
bool ans[21];
bool st[21];
void dfs(int u, int s){
// cout << u << endl;
if(s > mx){
mx = s;
for(int i = 1;i <= n;i++){
if(st[i])ans[i] = 1;
else ans[i] = 0;
}
}
for(auto i : e[u]){
// cout << 1 << endl;
st[i] = true;
dfs(i, s + w[i]);
st[i] = false;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++)cin >> w[i];
for(int i = 1;i <= n - 1;i++){
for(int j = i + 1;j <= n;j++){
int x;
cin >> x;
if(x){
e[i].push_back(j);
}
}
}
for(int i = 1;i <= n;i++){
st[i] = true;
dfs(i, w[i]);
st[i] = false;
}
for(int i = 1;i <= n;i++){
if(ans[i])cout << i << " ";
}
cout << endl;
cout << mx;
return 0;
}