forked from vineetyadav2403/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roads and Libraries.cpp
43 lines (42 loc) · 1007 Bytes
/
Roads and Libraries.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
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,n) for(int i=0;i<n;i++)
int n,m, lib,road;
void dfs(vector<int> a[],vector<bool> &visited,set<int> &s,int &i){
if(!visited[i]){
visited[i] = true;
s.insert(i);
for(auto j:a[i]){
dfs(a, visited,s,j);
}
}
}
int main(){
int t;
cin>>t;
while(t--){
long long int cost = 0;
cin>>n>>m>>lib>>road;
vector<int> a[n+1];
vector<bool> visited(n+1,false);
set<int> s;
FOR(i,m){
int x,y;
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=1;i<=n;i++){
dfs(a,visited,s,i);
if(s.size()!=0){
if(road < lib)
cost +=(road*(s.size() - 1))+lib;
else
cost = cost + (lib *(s.size() - 1))+lib;
}
s.clear();
}
cout<<cost<<endl;
}
return 0;
}