-
Notifications
You must be signed in to change notification settings - Fork 0
/
Road Construction.cpp
84 lines (84 loc) · 1.73 KB
/
Road Construction.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
#include<bits/stdc++.h>
using namespace std;
struct Edge
{
int u , v;
int cost;
};
vector< Edge > E;
bool operator<( Edge a , Edge b)
{
return a.cost < b.cost;
}
int parent[303], n;
int find_parent(int x)
{
if(parent[x]==x)
return x;
return parent[x] = find_parent(parent[x]);
}
int kruskal(int n)
{
for(int i = 1 ; i <= n ; i++)
parent[i] = i;
sort(E.begin() , E.end());
int sz = E.size();
int ans = 0;
for(int i = 0 ; i < sz ; i++)
{
if(find_parent(E[i].u)!=find_parent(E[i].v))
{
parent[parent[E[i].u]] = parent[E[i].v];
ans +=E[i].cost;
}
}
return ans;
}
int main()
{
int tc;
cin>>tc;
int cs = 0;
while(tc--)
{
int value = 0;
cs++;
cin>>n;
string u , v;
Edge e;
int cost;
map< string , int > mp;
for(int i = 0 ; i < n ; i++)
{
cin>>u>>v>>cost;
if(!mp[u])
{
mp[u] = ++value;
}
if(!mp[v])
{
mp[v] = ++value;
}
e.u = mp[u];
e.v = mp[v];
e.cost = cost;
E.push_back(e);
}
//cout<<value<<endl;
int res = kruskal(value);
set< int > s;
for(int i= 1 ; i <= value ; i++)
{
s.insert(find_parent(i));
}
if(s.size()==1)
cout<<"Case "<<cs<<": "<<res<<endl;
else
cout<<"Case "<<cs<<": Impossible"<<endl;
E.clear();
mp.clear();
s.clear();
memset(parent , 0 , sizeof(parent));
}
return 0;
}