Cod sursa(job #2337524)

Utilizator WayronUrsu Ianis-Vlad Wayron Data 6 februarie 2019 15:11:20
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <bits/stdc++.h>
using namespace std;
int n, m, p;

vector<vector<pair<int, int> > >graph;

vector<int> key;
vector<bool> inTree;
vector<int> father;

int minKeyIndex(){
    int minKey = INT_MAX, minIndex;
    for(auto i=1; i<=n; i++){
        if(inTree.at(i)==false && key.at(i)<minKey){
            minKey = key.at(i);
            minIndex = i;
        }
    }

    return minIndex;
}

int main()
{
    ifstream fin("radiatie.in");
    fin>>n>>m>>p;
    graph.resize(n+1, vector<pair<int, int> >());

    int x, y, c;
    for(auto i=1; i<=m; i++){
        fin>>x>>y>>c;
        graph.at(x).push_back(make_pair(y, c));
        graph.at(y).push_back(make_pair(x, c));
    }

    father.resize(n+1);
    key.resize(n+1, INT_MAX);
    inTree.resize(n+1, false);
    key.at(1)=0;

    for(auto i=1; i<n; i++){

        int minIndex = minKeyIndex();
        inTree.at(minIndex) = true;

        for(auto& neighbour:graph.at(minIndex)){
            if(inTree.at(neighbour.first)==false && (key.at(neighbour.first)>neighbour.second))
                key.at(neighbour.first) = neighbour.second, father.at(neighbour.first) = minIndex;
        }
    }

    inTree.clear();
    graph.clear();

    vector<vector<int> > m(n+1, vector<int>(n+1, INT_MAX));
    for(auto i=2; i<=n; i++){
        m[i][father.at(i)] = m[father.at(i)][i] = key.at(i);
    }

    for(auto i=1; i<=n; i++){
        for(auto j=1; j<=n; j++){
            if(i!=j && m[i][j]==INT_MAX){
                for(auto k=1; k<=n; k++)
                   if(k!=i && k!=j) m[i][j] = min(m[i][j], max(m[i][k], m[k][j]));
            }
        }
    }
    ofstream fout("radiatie.out");
    for(auto i=1; i<=p; i++){
        fin>>x>>y;
        if(x==y)fout<<0<<endl;
        fout<<m[x][y]<<endl;
    }
}