-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLOJ 1040 - Donation.cpp
88 lines (79 loc) · 1.65 KB
/
LOJ 1040 - Donation.cpp
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
83
84
85
86
87
88
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define ull unisgned long long
#define read freopen("Input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define vi vector<int>
#define MAX 55
using namespace std;
vector<pair <int, pii > > v;
int parent[MAX];
int root(int node)
{
if(parent[node]==node)
return node;
return parent[node]=root(parent[node]);
}
bool makeUnion(int a, int b)
{
int p,q;
p=root(a);
q=root(b);
if(p==q)
return false;
parent[p]=parent[q];
return true;
}
int findFirstMST(int totalNode)
{
if(totalNode==1)
return 0;
for(int i=0;i<=totalNode; i++)
parent[i]=i;
sort(v.begin(), v.end());
int tEdge=v.size();
int cost=0,used=0;
for(int i=0;i<tEdge; i++)
{
if(makeUnion(v[i].second.first, v[i].second.second))
{
cost+=v[i].first;
used++;
if(used==totalNode-1)
{
v.clear();
return cost;
}
}
}
v.clear();
return -1;
}
int main()
{
int testCase,x,y,m,n,k;
cin>>testCase;
for(int qq=1;qq<=testCase;qq++)
{
cin>>n;
int totCable=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
cin>>x;
totCable+=x;
if(x)
v.pb(mp(x, mp(i,j)));
}
int res=findFirstMST(n);
printf("Case %d: ", qq);
if(res==-1)
cout<<res<<endl;
else
cout<<totCable-res<<endl;
}
return 0;
}