Pagini recente » Cod sursa (job #2146029) | Cod sursa (job #1140494) | Cod sursa (job #2690332) | Cod sursa (job #1594342) | Cod sursa (job #1938234)
#include <bits/stdc++.h>
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
int n,m,k,i,x,y,L[1<<14],C[1<<14],T[1<<14],GR[1<<14];
bool viz[1<<14];
vector <pair <int,int> > G[1<<14];
struct pt
{
int x,y,c;
}v[1<<15];
bool cmp(pt a,pt b)
{
return a.c<b.c;
}
void dfs(int x)
{
viz[x]=1;
for(int i=0;i<G[x].size();++i)
if(!viz[G[x][i].first])
{
L[G[x][i].first]=L[x]+1;
C[G[x][i].first]=G[x][i].second;
T[G[x][i].first]=x;
dfs(G[x][i].first);
}
}
int gr(int i)
{
if(GR[i]!=i) GR[i]=gr(GR[i]);
return GR[i];
}
int main()
{
f>>n>>m>>k;
for(i=1;i<=m;++i) f>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,cmp);
for(i=1;i<=n;++i) GR[i]=i;
for(i=1;i<=m;++i)
if(gr(v[i].x)!=gr(v[i].y))
{
G[v[i].x].push_back(make_pair(v[i].y,v[i].c));
G[v[i].y].push_back(make_pair(v[i].x,v[i].c));
GR[gr(v[i].x)]=GR[v[i].y];
}
dfs(1);
while(k--)
{
f>>x>>y;
int cost=0;
while(T[x]!=T[y])
if(L[x]<L[y]) cost=max(cost,C[y]),y=T[y];
else cost=max(cost,C[x]),x=T[x];
while(x!=y)
if(L[x]<L[y]) cost=max(cost,C[y]),y=T[y];
else cost=max(cost,C[x]),x=T[x];
g<<cost<<'\n';
}
return 0;
}