Pagini recente » Cod sursa (job #1647837) | Cod sursa (job #2902230) | Cod sursa (job #1680092) | Cod sursa (job #1220260) | Cod sursa (job #2374667)
#include <bits/stdc++.h>
#define N 15005
#define f first
#define s second
using namespace std;
ifstream in("radiatie.in");
ofstream out("radiatie.out");
vector<pair<int,pair<int,int>>> a[N];
priority_queue<pair<pair<int,int>,pair<int,int>>>h;
int v[N],d[N];
pair<int,int>dp[N][20];
bool vm[N*2];
void dfs(int i){
v[i]=0;
for(auto it:a[i]){
if(vm[it.s.s] && v[it.f]){
d[it.f]=d[i]+1;
dp[it.f][0]={i,it.s.f};
dfs(it.f);
}
}
}
int main() {
int n,m,k,i,x,y,w,l,j,o;
in>>n>>m>>k;
for(i=1; i<=m; ++i){
in>>x>>y>>w;
a[x].push_back({y,{w,i}});
a[y].push_back({x,{w,i}});
}
h.push({{0,0},{1,0}});
while(!h.empty()){
i=h.top().s.f;
y=h.top().s.s;
w=-h.top().f.f;
x=h.top().f.s;
h.pop();
if(v[i]!=-1){
v[i]=-1;
if(y) vm[x]=1;
for(auto it:a[i]){
if(!v[it.f] || v[it.f]>it.s.f){
v[it.f]=it.s.f;
h.push({{-it.s.f, it.s.s},{it.f,i}});
}
}
}
}
l=log2(n);
dfs(1);
for(i=1; i<=l; ++i)
for(j=1; j<=n; ++j)
dp[j][i]={dp[dp[j][i-1].f][i-1].f, max(dp[dp[j][i-1].f][i-1].s, dp[j][i-1].s)};
while(k--){
in>>x>>y;
if(d[x]>d[y]) swap(x,y);
w=d[y]-d[x];
j=o=0;
for(i=l; i>=0 && w; --i)
if(w-(1<<i)>=0){
w-=(1<<i);
j=max(j,dp[y][i].s);
y=dp[y][i].f;
}
if(x==y){
out<<j<<"\n";
continue;
}
for(i=l; i>=0; --i){
if(dp[x][i].f!=dp[y][i].f){
o=max(o,dp[x][i].s);
x=dp[x][i].f;
j=max(j,dp[y][i].s);
y=dp[y][i].f;
}
}
out<<max(max(o,dp[x][0].s),max(j,dp[y][0].s))<<"\n";
}
return 0;
}