-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleCountMinimalLibraryCosts.cpp
More file actions
executable file
·69 lines (67 loc) · 1.84 KB
/
Copy pathCycleCountMinimalLibraryCosts.cpp
File metadata and controls
executable file
·69 lines (67 loc) · 1.84 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
class Node
{
public:
ll parentIndex;
ll index;
};
int main()
{
ll queries;
cin >> queries;
for(ll l = 0; l < queries; l++)
{
ll cities, roads, libraryCost, roadCost;
ll cyclesCount = 0;
cin >> cities >> roads >> libraryCost >> roadCost;
vector< Node > cityNodes(cities + 1);
for(ll i = 0; i <= cities; i++)
{
cityNodes[i].parentIndex = i;
cityNodes[i].index = i;
}
for(ll i = 0; i < roads; i++)
{
ll city1, city2;
cin >> city1 >> city2;
while(cityNodes[city1].parentIndex != cityNodes[city1].index)
{
city1 = cityNodes[city1].parentIndex;
}
while(cityNodes[city2].parentIndex != cityNodes[city2].index)
{
city2 = cityNodes[city2].parentIndex;
}
if(cityNodes[city1].parentIndex == cityNodes[city2].parentIndex)
{
cyclesCount += 1;
}
else
{
cityNodes[city2].parentIndex = cityNodes[city1].parentIndex;
}
}
ll librariesRequired = 0;
for(ll i = 1; i <= cities; i++)
{
if(cityNodes[i].index == cityNodes[i].parentIndex)
{
librariesRequired += 1;
}
}
// cout << cyclesCount << " " << librariesRequired << endl;
ll totalMinCost1 = librariesRequired * libraryCost + (roads - cyclesCount) * roadCost;
ll totalMinCost2 = cities * libraryCost;
if(totalMinCost1 < totalMinCost2)
{
cout << totalMinCost1 << endl;
}
else
{
cout << totalMinCost2 << endl;
}
}
return 0;
}