Pagini recente » Cod sursa (job #1336799) | Cod sursa (job #62406) | Cod sursa (job #1734232) | Cod sursa (job #1651744) | Cod sursa (job #1310808)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
#define PII pair<int, int>
#define x first
#define y second
ifstream is ("radiatie.in");
ofstream os ("radiatie.out");
int N, M, Qr;
vector <PII> G[15001], APM[15001];
int D[15001], T[15001], Depth[15001];
bool B[15001];
priority_queue <PII, vector<PII>, greater<PII>> Q;
void Read();
void Make_APM();
void Solve();
int FindLCA(int A, int B);
int main()
{
Read();
Make_APM();
Solve();
}
void Read()
{
is >> N >> M >> Qr;
for (int a, b, c; M; --M)
{
is >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
};
void Make_APM()
{
for (int i = 1; i <= N; ++i) D[i] = 1<<30;
D[1] = 0;
Q.push({0, 1});
for(int i; !Q.empty();)
{
i = Q.top().y;
B[i] = 1;
for (const auto& j : G[i])
if (B[j.x] == 0 && D[j.x] > j.y)
{
D[j.x] = j.y;
T[j.x] = i;
Depth[j.x] = Depth[i]+1;
Q.push({j.y, j.x});
}
if (i != 1)
APM[T[i]].push_back({i, D[i]}), APM[i].push_back({T[i], D[i]});
while (!Q.empty() && B[Q.top().y] == 1)
Q.pop();
}
};
void Solve()
{
for (int X, Y; Qr; --Qr)
{
is >> X >> Y;
os << FindLCA(X, Y) << '\n';
}
};
int FindLCA(int A, int B)
{
int val = 0;
while (Depth[A] > Depth[B])
{
val = max(val, D[A]);
A = T[A];
}
while (Depth[A] < Depth[B])
{
val = max(val, D[B]);
B = T[B];
}
while (A != B)
{
val = max(val, max(D[A], D[B]));
A = T[A], B = T[B];
}
return val;
};