-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEjercicio_3.cpp
118 lines (94 loc) · 2.44 KB
/
Ejercicio_3.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
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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct edge {
int u;
int v;
long long d;
long long r;
double costo;
bool es_treeEdge = false;
edge(int u, int v, long long d, long long r): u(u), v(v), d(d), r(r) {}
void aplicar_costo(double c) { costo = (d - (c * r)); }
};
int n, m;
vector<edge> edges;
long long suma_distancias = 0, suma_repetidores = 0;
struct DSU {
vector<int> p;
vector<int> rank;
DSU(int n){
p = vector<int>(n, -1);
rank = vector<int>(n, 1);
}
void unite(int u, int v){
int c1, c2;
c1 = find(u); c2 = find(v);
if(c1 == c2) return;
if(rank[c2] > rank[c1]) swap(c1, c2);
p[c2] = c1;
rank[c1] = max(rank[c1], rank[c2]+1);
}
int find(int u){
if(p[u] == -1) return u;
int rep = find(p[u]);
p[u] = rep;
return rep;
}
};
void kruskal(double c) {
suma_distancias = suma_repetidores = 0;
for(edge &e: edges) {
e.aplicar_costo(c);
e.es_treeEdge = false;
}
sort(edges.begin(), edges.end(), [](edge a, edge b) { return a.costo > b.costo; });
DSU dsu(n+1);
for(edge &e: edges) {
if(dsu.find(e.u) != dsu.find(e.v)) {
dsu.unite(e.u, e.v);
e.es_treeEdge = true;
suma_distancias += e.d;
suma_repetidores += e.r;
}
}
}
double calcular_costo(double c) {
double suma = 0;
for(edge &e: edges) {
if (e.es_treeEdge) suma += e.costo;
}
return suma;
}
void buscar_costo_optimo() {
double limite_inferior = 0;
double limite_superior = 10e6;
int cantidad_refinamiento = 60;
while(cantidad_refinamiento--) {
double c_medio = (limite_inferior + limite_superior) / 2;
kruskal(c_medio);
// Si el w(T) es mayor a 0 quiero probar con un c más grande
if (calcular_costo(c_medio) >= 0) {
limite_inferior = c_medio;
} else {
// Si no lo achicamos
limite_superior = c_medio;
}
}
}
int main() {
int c; cin >> c;
while(c--) {
cin >> n >> m;
edges.clear();
while(m--) {
int u, v;
long long d, r;
cin >> u >> v >> d >> r;
edges.push_back(edge(u,v,d,r));
}
buscar_costo_optimo();
cout << suma_distancias << " " << suma_repetidores << endl;
}
}