85 lines
1.9 KiB
C++
85 lines
1.9 KiB
C++
#include <algorithm>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
struct Node {
|
|
int mine_amount = -1, prev = -1;
|
|
};
|
|
|
|
constexpr int MAX_N = 25, INF = 0x3f3f3f3f;
|
|
int n, mine_amounts[MAX_N];
|
|
bool adj[MAX_N][MAX_N];
|
|
|
|
// int dp[MAX_N][MAX_N];
|
|
Node dp_route[MAX_N];
|
|
|
|
void solve_dp() {
|
|
for (int i=0; i<n; i++) {
|
|
dp_route[i] = {.mine_amount = mine_amounts[i]};
|
|
}
|
|
for (int current = 1; current < n; current++) {
|
|
for (int prev = 0; prev < current; prev++) {
|
|
if (adj[prev][current]) {
|
|
int new_ans = dp_route[prev].mine_amount + mine_amounts[current];
|
|
if (new_ans > dp_route[current].mine_amount) {
|
|
dp_route[current] = {.mine_amount = new_ans, .prev = prev};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void print_route() {
|
|
int max_ans = -1, max_idx = -1;
|
|
vector<int> final_route;
|
|
for (int i = 0; i < n; i++) {
|
|
if (dp_route[i].mine_amount > max_ans) {
|
|
max_ans = dp_route[i].mine_amount;
|
|
max_idx = i;
|
|
}
|
|
}
|
|
while (max_idx != -1) {
|
|
final_route.push_back(max_idx);
|
|
max_idx = dp_route[max_idx].prev;
|
|
}
|
|
reverse(final_route.begin(), final_route.end());
|
|
for (const int i : final_route) {
|
|
cout << i + 1 << " ";
|
|
}
|
|
cout << endl << max_ans << endl;
|
|
}
|
|
|
|
void log_dp() {
|
|
for (int i = 0; i < n; i++) {
|
|
// for (int j = 0; j < n; j++) {
|
|
// clog << dp[i][j] << " ";
|
|
// }
|
|
clog << "->" << dp_route[i].mine_amount << " " << dp_route[i].prev << endl;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
cin >> n;
|
|
for (int i = 0; i < n; i++) {
|
|
cin >> mine_amounts[i];
|
|
}
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = i + 1; j < n; j++) {
|
|
cin >> adj[i][j];
|
|
adj[j][i] = adj[i][j];
|
|
}
|
|
}
|
|
|
|
solve_dp();
|
|
log_dp();
|
|
print_route();
|
|
|
|
return 0;
|
|
}
|