dij
meda
c_cpp
10 months ago
1.6 kB
6
Indexable
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
template<class T>
void printff(vector<T>& v) {
for (auto k : v) cout << k << " ";
cout << endl;
}
int n;
vector<vector<pair<int, ll>>> graph;
vector<ll> dijkstra(int node){
vector<ll> ret(n + 1, 1e15);
ret[node] = 0;
using pi = pair<ll, int>; // dist,node
priority_queue<pi, vector<pi>, greater<pi>> pq;
pq.push({0, node});
while (!pq.empty()){
ll cost = pq.top().first;
node = pq.top().second;
pq.pop();
if (ret[node] != cost)
continue;
for (auto adj : graph[node]){
ll w = cost + adj.second;
if (ret[adj.first] > w){
ret[adj.first] = w;
pq.push({w, adj.first});
}
}
}
return ret;
}
void SOLVE() {
int m; cin >> n >> m;
graph = vector<vector<pair<int, ll>>>(n + 1);
vector<pair<int, int>> edges;
for(int i = 0, u, v, w; i < m; i++){
cin >> u >> v >> w;
graph[u].push_back({v, w});
graph[v].push_back({u, w});
edges.push_back({u, v});
}
map<pair<int, int>, ll> answer;
vector<ll> a;
for(int i = 1; i <= n; i++){
for(auto &[v, w] : graph[i]){
ll temp = w;
w = 1e15;
a = dijkstra(i);
answer[{i, v}] = (a[v] >= 1e15 ? - 1 : a[v]);
w = temp;
}
}
for(int i = 0; i < m; i++){
cout << answer[edges[i]] << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 1; //cin >> tc;
while(tc--) SOLVE();
return 0;
}Editor is loading...
Leave a Comment