-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLOJ-1041.cpp
48 lines (48 loc) · 1.03 KB
/
LOJ-1041.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
#include<bits/stdc++.h>
struct edge{
int u,v,w;
bool operator <(const edge&p)const{
return w<p.w;
}edge(int a,int b,int c){
u=a,v=b,w=c;
}
};
int pr[60];
int parent(int r){
return (pr[r]==r)?r:pr[r]=parent(pr[r]);
}
using namespace std;
int main(){
//freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
for(int z=1;z<=t;z++){
map<string,int>mp;
string s1,s2;
vector<edge>v;
int cost,m,cnt=1;
scanf("%d",&m);
while(m--){
cin>>s1>>s2>>cost;
if(mp[s1]==0){
mp[s1]=cnt++;
}if(mp[s2]==0){
mp[s2]=cnt++;
}v.push_back(edge(mp[s1],mp[s2],cost));
}sort(v.begin(),v.end());
for(int i=0;i<cnt;i++)pr[i]=i;
cnt--;
int sum=0,l=v.size();
for(int i=0;i<l;i++){
int x=parent(v[i].u),y=parent(v[i].v);
if(x!=y){
pr[x]=y;
cnt--;
sum+=v[i].w;
if(cnt==0)break;
}
}if(cnt>1)cout<<"Case "<<z<<": Impossible\n";
else cout<<"Case "<<z<<": "<<sum<<"\n";
}
return 0;
}