-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11733timeLimit.cpp
More file actions
136 lines (120 loc) · 2.08 KB
/
11733timeLimit.cpp
File metadata and controls
136 lines (120 loc) · 2.08 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<bits/stdc++.h>
using namespace std;
struct edge{
int x;
int y;
int w;
edge(int a,int b,int c){
x = a;
y = b;
w = c;
}
bool operator < (const edge& p) const{
return this->w < p.w;
}
};
int numOfVertices;
int numOfEdges;
int airportCost;
vector<edge>edges;
map<int,int>par;
map<int,int>rep;
map<int,int>repV;
set<int>repSet;
vector<int>costs;
void clear(){
edges.clear();
par.clear();
rep.clear();
repSet = set<int>();
costs.clear();
repV.clear();
}
void makeSet(int x){
par[x] =x;
}
int Find(int x){
if(par[x] == x){
return x;
}
par[x] = Find(par[x]);
return par[x];
}
void Union(int a,int b){
int x = Find(a);
int y = Find(b);
if(x != y){
par[y] = x;
}
}
int main(){
int tt;
cin>>tt;
int cnt = 0;
while(tt--){
cin>>numOfVertices>>numOfEdges>>airportCost;
int tv = numOfVertices;
int te = numOfEdges;
for(int i = 1;i<=tv;i++){
makeSet(i);
repV[i] = 0;
}
while(te--){
int x,y,z;
cin>>x>>y>>z;
if(z < airportCost){
edges.push_back(edge(x,y,z));
Union(x,y);
}else{
par[x] = x;
par[y] = y;
}
}
for(int i = 1;i<=tv;i++){
rep[i] = Find(i);
//cout<<"rep["<<i<<"] = "<<rep[i]<<endl;
repSet.insert(rep[i]);
repV[rep[i]]++;
}
/*
for(auto i:repSet){
cout<<"repV["<<i<<"] = "<<repV[i]<<endl;
}
*/
int s;
int cunt;
for(auto i:repSet){
for(int k = 1;k<= tv;k++){
par[k] = k;
}
cunt = 0;
s = 0;
sort(edges.begin(),edges.end()); // it prevents repetation sweet khushkal ....
for(int j = 0;j<(int)edges.size();j++){
if( rep[ edges[j].x ] == i && rep[ edges[j].y ] == i ){
int u = Find(edges[j].x);
int v= Find(edges[j].y);
if(u != v){
par[u] = v;
cunt++;
s += edges[j].w;
if(cunt == repV[rep[i]] - 1){
break;
}
}
}
}
if(cunt == repV[rep[i]] - 1){
costs.push_back(s);
}
}
/*
for(auto i:costs){
cout<<i<<' ';
}
cout<<endl;
*/
cout<<"Case #"<< ++cnt <<": "<< ( accumulate(costs.begin(),costs.end(),0)+ ((int)repSet.size()*airportCost) ) <<" "<< repSet.size() <<endl;
clear();
}
}