-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1982.cpp
More file actions
82 lines (74 loc) · 1.9 KB
/
1982.cpp
File metadata and controls
82 lines (74 loc) · 1.9 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
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
#include <iostream>
#define int long long
const int inf = 1e9 + 7;
using namespace std;
signed main() {
int n, k;
cin >> n >> k;
int** mas = new int* [n];
for (int i = 0; i < n; i++)
mas[i] = new int[n];
vector <int> a(k);
for (int i = 0; i < k; i++) {
cin >> a[i];
a[i]--;
}
int** vis = new int* [n];
for (int i = 0; i < n; i++)
vis[i] = new int[n];
vector <int> min_e(n, inf);
vector <int> sel_e(n, -1);
vector <bool> used(n, false);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> mas[i][j];
if (find(a.begin(), a.end(), i) != a.end() and find(a.begin(), a.end(), j) != a.end()) {
mas[i][j] = 0;
mas[j][i] = 0;
}
vis[i][j] = 0;
}
min_e[i] = inf;
sel_e[i] = -1;
used[i] = false;
}
int v = -1;
for (int i = 0; i < n; i++) {
v = -1;
for (int j = 0; j < n; j++) {
if (!used[j] and v == -1) {
v = j;
}
if (v != -1) {
if (!used[j] and min_e[j] < min_e[v] and j != v) {
v = j;
}
}
}
if (v != -1) {
used[v] = true;
for (int to = 0; to < n; to++) {
if (!used[to] and mas[v][to] < min_e[to] and to != v) {
min_e[to] = mas[v][to];
sel_e[to] = v;
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
v = sel_e[i];
if (v != -1) {
if (vis[i][v] == 0) {
ans = ans + min_e[i];
vis[i][v] = 1;
vis[v][i] = 1;
}
}
}
if (n == 1) cout << 0 << endl;
else cout << ans << endl;
return 0;
}