Cod sursa(job #2172471)

Utilizator razvanlgu31Razvan Lungu razvanlgu31 Data 15 martie 2018 16:38:58
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <fstream>
#include <vector>
#include <queue>
#include <functional>
#define M 2000000000
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int n, m, k, x, y, c, viz[15010], grad[15010], i, Max, a[20][15010], b[20][15010], j, p;
struct tata
{
    int c, nod;
} t[15010];
vector < pair < int, int > > g[30010];
priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int, int > > > qu;
void apm()
{
    for(int j = 2; j <= n; j++)
    {
        t[j].c = M;
    }
    qu.push({0, 1});
    while(!qu.empty())
    {
        int nod = qu.top().second;
        qu.pop();
        if(viz[nod] == 0)
        {
            viz[nod] = 1;
            for(int i = 0; i < (int)g[nod].size(); i++)
            {
                if(viz[g[nod][i].first] == 0)
                {
                    if(t[g[nod][i].first].c > g[nod][i].second)
                    {
                        t[g[nod][i].first].c = g[nod][i].second;
                        t[g[nod][i].first].nod = nod;
                        grad[g[nod][i].first] = grad[nod] + 1;
                    }
                    qu.push({t[g[nod][i].first].c, g[nod][i].first});
                }
            }
        }
    }

}

int main()
{
    fin >> n >> m >> p;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        g[x].push_back({y, c});
        g[y].push_back({x, c});
    }
    apm();
    for(i = 1; i <= n; i++)
    {
        a[0][i] = t[i].nod;
        b[0][i] = t[i].c;
    }
    for(j = 1; (1 << j) <= n; j++)
        for(i = 1; i <= n; i++)
        {
            a[j][i] = a[j-1][a[j-1][i]];
            b[j][i] = max(b[j-1][i], b[j-1][a[j-1][i]]);
        }
    for(i = 1; i <= p; i++)
    {
        fin >> x >> y;
        Max = 0;
        if(grad[x] < grad[y])
            swap(x, y);
            if(grad[x] > grad[y])
            {
                for(k = 1; (1 << (k-1)) <= n; k++)
                    for(j = k; j >= 0; j--)
                    {
                        if(grad[a[j][x]] >= grad[y])
                        {
                            Max = max( Max, b[j][x] );
                            x = a[j][x];
                        }
                    }
            }
        if(x == y)
        {
            fout << Max << '\n';
        }
        else
        {
            for(k = 1; (1 << (k-1)) <= n; k++)
            for(j = k; j >= 0; j--)
                {
                        if(a[j][x] != a[j][y])
                        {
                            Max = max( Max, b[j][x] );
                            Max = max( Max, b[j][y] );
                            x = a[j][x];
                            y = a[j][y];
                        }
                }
            Max = max( Max, b[0][x] );
            Max = max( Max, b[0][y] );
            fout << Max << '\n';
        }
    }
    return 0;
}