Cod sursa(job #2339273)

Utilizator victorv88Veltan Victor victorv88 Data 8 februarie 2019 17:08:03
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.1 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

#define MAX 1000000005
using namespace std;

ifstream f("radiatie.in");
ofstream g("radiatie.out");

int n, m, k, father[15005], height[15005], from ,to, val_max;

bool ok, viz[15005];

vector<pair<int,int>>graph[15005];

struct d{
    int from, to, distanta;
}drumuri[30005];

bool cmp(d a, d b)
{
    return a.distanta<b.distanta;
}

void citire()
{
    f >> n >> m >> k;
    for (int dr=1; dr<=m; ++dr)
    {
        f >> drumuri[dr].from >> drumuri[dr].to >> drumuri[dr].distanta;
    }
    sort(drumuri+1,drumuri+n+1, cmp);
}

int find_father(int ind)
{
    if (ind==father[ind])
    {
        return ind;
    }
    father[ind]=find_father(father[ind]);
    return father[ind];
}

void unite(int ind1, int ind2)
{
    if (height[ind1]>height[ind2])
    {
        father[ind2]=find_father(ind1);
    }
    else{
        father[ind1]=find_father(ind2);
    }
    if (height[ind1]==height[ind2])
    {
        height[ind2]++;
    }
}

void creeare_apm()
{
    for (int i=1; i<=n; i++)
    {
        father[i]=i;
    }
    for (int i=1; i<=m; i++)
    {
        if (find_father(drumuri[i].from)!=find_father(drumuri[i].to))
        {
            unite(drumuri[i].from,drumuri[i].to);
            graph[drumuri[i].from].push_back({drumuri[i].to,drumuri[i].distanta});
            graph[drumuri[i].to].push_back({drumuri[i].from,drumuri[i].distanta});
        }
    }
}

void dfs(int ind, int &maxi, int prev)
{
    if (ind==to)
    {
        val_max=maxi;
        ok=false;
        return;
    }
    if (!ok)
        return;
    viz[ind]=1;
    for (auto &v:graph[ind])
    {
        if (viz[v.first]==0)
        {
            if (v.second>prev)
                maxi=v.second,prev=v.second;
            dfs(v.first,maxi,maxi);

        }
    }
}

void rezolvare_query()
{
    int maxi;
    for (int i=0; i<k; ++i)
    {
        memset(viz,0,n+1);
        ok=true;
        maxi=0;
        f >> from >> to;
        dfs(from,maxi,0);
        g << val_max << '\n';
    }
}

int main() {
    citire();
    creeare_apm();
    rezolvare_query();
    return 0;
}