Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- const int INF = 1e9;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int n;
- cin >> n;
- vector<vector<int>> gr(n, vector<int>(n));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- cin >> gr[i][j];
- }
- }
- vector<vector<int>> dp(1 << n, vector<int>(n, INF));
- for (int i = 0; i < n; ++i) {
- dp[1 << i][i] = 0;
- }
- for (int mask = 1; mask < (1 << n); ++mask) {
- for (int i = 0; i < n; ++i) {
- if (!(mask & (1 << i))) {
- continue;
- }
- for (int j = 0; j < n; ++j) {
- if (mask & (1 << j)) {
- continue;
- }
- dp[mask | (1 << j)][j] = min(dp[mask | (1 << j)][j], dp[mask][i] + gr[i][j]);
- }
- }
- }
- cout << *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end()) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement