-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoadsandLibraries.cpp
More file actions
128 lines (81 loc) · 1.72 KB
/
RoadsandLibraries.cpp
File metadata and controls
128 lines (81 loc) · 1.72 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
#include <bits/stdc++.h>
using namespace std;
int mx = INT_MAX;
const int arr_limit = 1e7+10;
//int arr[arr_limit];
const int graph_v_limit = 1e5+10;
vector<int> g[graph_v_limit];
vector<int> current_cc;
bool visited[graph_v_limit];
void dfs(int vertex){
// cout << vertex<< endl;
visited[vertex] = 1;
current_cc.push_back(vertex);
for(auto child : g[vertex]){
if(visited[child]) continue;
dfs(child);
}
}
bool has_cycle(int vertex){
cout << vertex<< endl;
visited[vertex] = 1;
for(auto child : g[vertex]){
if(visited[child]){
cout<<" making cycle";
return true;
}
has_cycle(child);
}
cout << " oustside for";
return false;
}
void clear_graph(int n){
for(int i=1;i<=n;++i){
g[i].clear();
visited[i]=0;
}
}
int main(){
// int l = sizeof(g)/sizeof(g[0]);
// cout<< "hello world " << l << typeid(g[0]).name() << endl;
// cout<< mx;
int t;
cin >> t;
while(t--){
// vector<int> g[graph_v_limit];
// g.clear()
// for (auto& v : g) {
// v.clear();
// }
// bool visited[graph_v_limit];
// visited.clear()
int n,m,c_lib,c_road;
cin >> n >> m>>c_lib>>c_road;
// cout << n<<" "<<m<< " "<<c_lib<<" "<<c_road<<endl
for(int i=0; i<m;++i){
int v1,v2;
cin >> v1 >>v2;
g[v1].push_back(v2);
g[v2].push_back(v1);
}
if(c_lib<c_road || m==0) {
// cout << " inside";
cout << c_lib*1LL*n<<endl;
clear_graph(n);
continue;
}
// cout << has_cycle(0) << endl;
int connected_cities = 0;
long long int cost = 0;
for(int i=1;i<=n;++i){
if(visited[i]) continue;
current_cc.clear();
dfs(i);
connected_cities++;
cost+=(c_lib + ((current_cc.size()-1)*1LL*c_road));
}
cout << cost<<endl;
clear_graph(n);
}
return 0;
}