Cod sursa(job #3277194)

Utilizator crina2120Arnautu Cristina-Crina crina2120 Data 15 februarie 2025 13:32:07
Problema Radiatie Scor 20
Compilator cpp-64 Status done
Runda vs11_12_vine_oji_2025 Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("radiatie.in");
ofstream fout("radiatie.out");

int n, m, k, a[15003][15003], d[15003];
vector < pair<int, int> > g[15003];

void Init()
{
    for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			a[i][j] = 2e9;
}

void BFS(int x)
{
    int i, c;
    queue < pair <int, int> > q;
    for (i = 1; i <= n; i++)
        d[i] = 2e9;
    d[x] = 0;
    q.push({x, 0});
    while (!q.empty())
    {
        i = q.front().first;
        c = q.front().second;
        q.pop();
        for (auto j : g[i])
            if (d[j.first] > max(c, j.second))
			{
				d[j.first] = max(c, j.second);
				q.push({j.first, d[j.first]});
			}
    }
    for (i = 1; i <= n; i++)
		a[i][x] = a[x][i] = d[i];
}

int main()
{
    int i, x, y, c;
    fin >> n >> m >> k;
    Init();
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        g[x].push_back({y, c});
        g[y].push_back({x, c});
        a[x][y] = a[y][x] = c;
    }
    for (i = 1; i <= k; i++)
    {
		fin >> x >> y;
		if (a[x][y] == 2e9) BFS(x);
		fout << a[x][y] << "\n";
    }
    return 0;
}