-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCT006 CHU TRÌNH HAMILTON 3.cpp
More file actions
75 lines (75 loc) · 1.52 KB
/
CT006 CHU TRÌNH HAMILTON 3.cpp
File metadata and controls
75 lines (75 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
int a[505][505];
int n, st;
set<int> se[1005];
int x[1005];
bool used[1005];
vector<int> tmp;
int sum = 1e9;
void hamliton(int u)
{
for (int y : se[x[u - 1]])
{
if (u == n + 1 && y == st)
{
int tong = 0;
for (int i = 1; i < n; i++)
{
tong += a[x[i]][x[i + 1]];
}
tong += a[x[n]][x[1]];
if (tong < sum)
{
tmp.clear();
sum = tong;
tmp.push_back(st);
for (int i = 1; i <= n; i++)
{
tmp.push_back(x[i]);
}
tmp.push_back(st);
}
}
else if (!used[y])
{
x[u] = y;
used[y] = true;
hamliton(u + 1);
used[y] = false;
}
}
}
int main()
{
// freopen("CT.INP", "r", stdin);
// freopen("CT.OUT", "W", stdout);
cin >> n >> st;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
if (a[i][j] != 0 && a[i][j] != 10000)
{
se[i].insert(j);
se[j].insert(i);
}
}
}
x[1] = st;
used[x[1]] = true;
hamliton(2);
if (tmp.size() != 0)
{
cout << sum << endl;
for (int i = 1; i < tmp.size(); i++)
{
cout << tmp[i] << " ";
}
}
else
{
cout << 0;
}
}