-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLOJ-1059.cpp
41 lines (40 loc) · 957 Bytes
/
LOJ-1059.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
#include<bits/stdc++.h>
using namespace std;
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[10010];
int parent(int r){return (pr[r]==r)?r:pr[r]=parent(pr[r]);}
int main(){
int t;
freopen("input.txt","r",stdin);
scanf("%d",&t);
for(int z=1;z<=t;z++){
int n,m,a;
scanf("%d%d%d",&n,&m,&a);
vector<edge>v;
for(int i=0;i<=n;i++)pr[i]=i;
for(int i=0;i<m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
v.push_back(edge(a,b,c));
}sort(v.begin(),v.end());
long long sum=0,cnt=0,l=v.size(),ac=0;
for(int i=0;i<l;i++){
int x=parent(v[i].u),y=parent(v[i].v);
if(x!=y && v[i].w<a){
pr[x]=y;
cnt++;
sum+=v[i].w;
if(cnt==(n-1))break;
}
}for(int i=1;i<=n;i++){
if(pr[i]==i)ac++;
}sum+=(a*(ac));
cout<<"Case "<<z<<": "<<sum<<" "<<ac<<"\n";
}
return 0;
}