#include using namespace std; //https://www.geeksforgeeks.org/dsa/johnsons-algorithm/ const long long INF = 1e15; struct Edge { int to; int weight; }; typedef pair pii; // Dijkstra's algorithm for non-negative edge weights vector Dijkstra(int V, const vector>& adj, int src) { vector dist(V, INF); priority_queue, greater> pq; dist[src] = 0; pq.push({0, src}); while (!pq.empty()) { long long d = pq.top().first; int u = pq.top().second; // Fixed: added .top() pq.pop(); if (d > dist[u]) continue; for (auto& edge : adj[u]) { if (dist[u] + edge.weight < dist[edge.to]) { dist[edge.to] = dist[u] + edge.weight; pq.push({dist[edge.to], edge.to}); } } } return dist; } // Bellman-Ford to find h[] and detect negative cycles vector BellmanFord(int V, const vector>& edges, bool& hasCycle) { vector h(V + 1, INF); h[V] = 0; vector> all_edges = edges; for (int i = 0; i < V; i++) all_edges.push_back({V, i, 0}); for (int i = 0; i < V; i++) { for (auto& e : all_edges) { if (h[e[0]] != INF && h[e[0]] + e[2] < h[e[1]]) { h[e[1]] = h[e[0]] + e[2]; } } } hasCycle = false; for (auto& e : all_edges) { if (h[e[0]] != INF && h[e[0]] + e[2] < h[e[1]]) { hasCycle = true; return {}; } } h.pop_back(); return h; } vector> JohnsonAlgorithm(int V, const vector>& edgeList) { bool hasCycle; vector h = BellmanFord(V, edgeList, hasCycle); if (hasCycle) { cout << "The graph contains a negative weight cycle. Algorithm cannot proceed." << endl; return {{}}; } // Reweight edges to be non-negative vector> adj(V); for (auto& e : edgeList) { int u = e[0], v = e[1], w = e[2]; adj[u].push_back({v, (int)(w + h[u] - h[v])}); } vector> resultMatrix(V, vector(V)); // Run Dijkstra for every vertex for (int s = 0; s < V; s++) { vector d_prime = Dijkstra(V, adj, s); for (int v = 0; v < V; v++) { if (d_prime[v] == INF) resultMatrix[s][v] = INF; else resultMatrix[s][v] = d_prime[v] + h[v] - h[s]; } } return resultMatrix; } int main(){ using ll=long long; int n,m; cin>>n>>m; vector p(n); for (int i=0;i>p[i]; vector> edge; for (int i=0;i>u>>v>>t; u--;v--; edge.push_back({u,v,t}); } auto dist=JohnsonAlgorithm(n,edge); pair ans={INF,0}; for (int i=0;idist[i][j]){ ans.first=dist[i][j]; ans.second=1; } } cout<