Untitled
#include <bits/stdc++.h> using namespace std; #define ll long long int tt, tc; const int N = 309; pair<short, short> edges[N * N + N]; vector<pair<short, int>> g[N]; short exists[N][N], n; int m, dist[N]; int remove(short from, short to) { exists[from][to] = 0; exists[to][from] = 0; priority_queue<pair<int, short>> pq; // first is distance, second is node for (int i = 1; i <= n; i++) dist[i] = 1e9; dist[from] = 0; pq.emplace(0, from); while (!pq.empty()) { auto [d, v] = pq.top(); pq.pop(); d = -d; if (dist[v] != d) continue; for (auto& [u, weight] : g[v]) if (exists[v][u] && dist[u] > dist[v] + weight) { dist[u] = dist[v] + weight; pq.emplace(-dist[u], u); } } exists[from][to] = 1; exists[to][from] = 1; return dist[to]; } int convert(int distance) { if (distance < 1e9) return distance; return -1; } void solve() { cin >> n >> m; for (int i = 0; i < m; i++) { short u, v; int l; cin >> u >> v >> l; edges[i] = {u, v}; exists[u][v] = 1; exists[v][u] = 1; g[u].emplace_back(v, l); g[v].emplace_back(u, l); } for (int i = 0; i < m; i++) cout << convert(remove(edges[i].first, edges[i].second)) << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); tt = 1, tc = 1; // cin >> tt; while (tt--) { solve(); tc++; } }
Leave a Comment