Pagini recente » Cod sursa (job #757616) | Cod sursa (job #3276619) | Cod sursa (job #1963715) | Cod sursa (job #1406853) | Cod sursa (job #2711866)
#include <bits/stdc++.h>
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
inline void Boost(){
ios::sync_with_stdio(false);
f.tie(NULL);g.tie(NULL);
}
const int NMAX = 15005;
const int inf = 1e9;
int n,m,Q;
int dist[NMAX],viz[NMAX], from[NMAX], grad[NMAX], father[NMAX], lvl[NMAX];
vector < pair < int, int > > v[NMAX], nv[NMAX];
priority_queue < pair < int, int > > q;
int main(){
Boost();
int i,j,x,y,z,node,ans;
f >> n >> m >> Q;
for(i = 1 ; i <= m ; i++){
f >> x >> y >> z;
v[x].push_back({y, z});
v[y].push_back({x, z});
}
for(i = 1 ; i <= n ; i++)
dist[i] = inf;
q.push({0,1});
lvl[1] = 1;
from[1] = -1;
while(!q.empty()){
node = q.top().second;
q.pop();
if(viz[node])
continue;
viz[node] = 1;
if(from[node] != -1){
nv[node].push_back({from[node], dist[node]});
nv[from[node]].push_back({node, dist[node]});
}
for(auto it: v[node]){
if(viz[it.first]) continue;
if(dist[it.first] > it.second){
dist[it.first] = it.second;
from[it.first] = node;
lvl[it.first] = lvl[node] + 1;
q.push({-it.second, it.first});
}
}
}
while(Q--){
f >> x >> y;
ans = 0;
while(lvl[x] != lvl[y]){
if(lvl[x] > lvl[y]){
ans = max(ans, dist[x]);
x = from[x];
}else{
ans = max(ans, dist[y]);
y = from[y];
}
}
g << ans << "\n";
}
return 0;
}