Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- using namespace std;
- int n;
- bool used[10];
- int m[10][10];
- int ans = 1000000000;
- void work(vector<int>& p) {
- int curr = 0;
- for (int i = 0; i < n; ++i) {
- int val = m[p[i]][p[i + 1]];
- if (val != 0) curr += val;
- else return;
- }
- ans = min(curr, ans);
- }
- void go(vector<int>& p) {
- if (p.size() == n) {
- p.push_back(p[0]);
- work(p);
- p.pop_back();
- } else {
- for (int i = 0; i < n; ++i) {
- if (!used[i]) {
- used[i] = true;
- p.push_back(i);
- go(p);
- p.pop_back();
- used[i] = false;
- }
- }
- }
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- cin >> n;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- cin >> m[i][j];
- }
- }
- vector<int> p;
- memset(used, 0, n * sizeof(bool));
- go(p);
- if (ans != 1000000000) {
- cout << ans;
- } else if (n == 1 ){
- cout << "0";
- } else {
- cout << "-1";
- }
- cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement