Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define debug(l) cerr<<#l<<' '<<l<<'\n';
- #include "bits/stdc++.h"
- using namespace std;
- #define all(mask) mask.begin(), mask.end()
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef pair<int, int> pii;
- typedef long double ld;
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- map<int, int> log;
- vector<vector<int>>ed;
- int n;
- cin >> n;
- vector<vector<int>> gr;
- gr.resize(n);
- ed.resize(n, vector<int>(n));
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cin >> ed[i][j];
- if (ed[i][j] > 0) {
- gr[i].push_back(j);
- }
- }
- }
- vector<vector<pll>> dp(1ll << n, vector<pll>(n, { 1e13,1e13 }));
- dp[1][0] = { 0,-1 };
- for (ll mask = 1; mask < (1ll << n); mask++) {
- for (ll from = 0; from < n; from++) {
- for (int& to : gr[from]) {
- if (((1ll << to) & mask) || !((1ll << from) & mask)) continue;
- dp[mask | (1ll << to)][to] = min(dp[mask | (1ll << to)][to], { dp[mask][from].first + ed[from][to], from });
- }
- }
- }
- pll ans = { 1e13,-1 };
- ll last = (1ll << n) - 1ll;
- ll v;
- for (ll i = 0; i < n; i++) {
- ans = min(ans, dp[last][i]);
- if (ans == dp[last][i]) {
- v = i;
- }
- }
- if (ans.first == 1e13) {
- cout << -1 << '\n';
- return 0;
- }
- cout << ans.first << '\n';
- vector<ll> par;
- while (v != -1) {
- par.push_back(v);
- ll x = v;
- v = dp[last][v].second;
- last ^= (1ll << x);
- }
- reverse(all(par));
- for (ll& i : par) {
- cout << i + 1 << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement