-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path107.cpp
More file actions
62 lines (56 loc) · 1.18 KB
/
Copy path107.cpp
File metadata and controls
62 lines (56 loc) · 1.18 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
#include <vector>
#include <queue>
#include <iostream>
#include <utility>
#include <cstdlib>
using namespace std;
#define INF 1 << 30
#define MAX 40
#define PII pair<int, int>
#define MP make_pair
int prim(vector<vector<PII> > graph, int start, int N)
{
priority_queue<PII,
vector<PII >,
greater<PII > > pq;
int mst_count, mst_weight;
vector<bool> mst_seen(N + 1, false);
pq.push(MP(0, start));
mst_count = mst_weight = 0;
while (!pq.empty())
{
PII u = pq.top(); pq.pop();
if (mst_seen[u.second]) continue;
mst_seen[u.second] = true;
mst_count++;
mst_weight += u.first;
vector<PII> adj = graph[u.second];
for (int i = 0; i < adj.size(); i++)
if (!mst_seen[adj[i].first])
pq.push(MP(adj[i].second, adj[i].first));
if (mst_count == N)
break;
}
return mst_weight;
}
int main()
{
int N, total = 0;
string s;
cin >> N;
vector<vector<PII> > graph(N + 1);
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
cin >> s;
if (s[0] == '-') continue;
int val = atoi(s.c_str());
graph[i].push_back(MP(j, val));
total += val;
}
}
total /= 2; // undirected graph
cout << total - prim(graph, 1, N) << endl;
return 0;
}