-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfl.cpp
More file actions
67 lines (58 loc) · 2.19 KB
/
Copy pathfl.cpp
File metadata and controls
67 lines (58 loc) · 2.19 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
#include <bits/stdc++.h>
using namespace std;
const long long INF = (1LL<<60);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
// 1) Matriz de distancias: arranca con INF y 0 en la diagonal
vector<vector<long long>> dist(N+1, vector<long long>(N+1, INF));
for (int i = 1; i <= N; ++i) dist[i][i] = 0;
// 2) Leer aristas (no dirigido). Si tu grafo fuera dirigido, quita la línea simétrica.
for (int e = 0; e < M; ++e) {
int u, v; long long w;
cin >> u >> v >> w;
dist[u][v] = min(dist[u][v], w);
dist[v][u] = min(dist[v][u], w);
}
// --- TRACES: imprime la matriz
auto printMatrix = [&](const string& title){
cout << "\n" << title << "\n";
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (dist[i][j] >= INF/2) cout << "INF ";
else cout << dist[i][j] << " ";
}
cout << "\n";
}
};
printMatrix("Matriz inicial");
// 3) Floyd–Warshall: k = intermedio, i = origen, j = destino
for (int k = 1; k <= N; ++k) {
cout << "\n=== Usando k = " << k << " como intermedio ===\n";
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (dist[i][k] >= INF/2 || dist[k][j] >= INF/2) continue; // no hay camino via k
long long candidato = dist[i][k] + dist[k][j];
if (candidato < dist[i][j]) {
cout << "Mejora: dist[" << i << "][" << j << "] "
<< "pasa de " << (dist[i][j] >= INF/2 ? string("INF") : to_string(dist[i][j]))
<< " a " << candidato
<< " via " << i << "->" << k << "->" << j << "\n";
dist[i][j] = candidato;
}
}
}
printMatrix("Despues de k = " + to_string(k));
}
cout << "\nMatriz final de distancias mas cortas:\n";
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (dist[i][j] >= INF/2) cout << "INF ";
else cout << dist[i][j] << " ";
}
cout << "\n";
}
return 0;
}