Cod sursa(job #1512716)

Utilizator andrei.arnautuAndi Arnautu andrei.arnautu Data 28 octombrie 2015 15:42:32
Problema Radiatie Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.65 kb
/**
  *  Worg
  */
#include <queue>
#include <cstdio>
#include <vector>
#include <algorithm>

#define pb          push_back

using namespace std;
FILE *fin = freopen("radiatie.in", "r", stdin);
FILE *fout = freopen("radiatie.out", "w", stdout);

const int MAX_N = 1 + 15000,
          bufferDim = 10000;

class inputReader {

private:
        int pos;
        char buffer[bufferDim];
public:

        void getBuffer() {

            pos= 0;
            fread(buffer, 1, bufferDim, stdin);
        }

        bool digit(char c) {

            return ('0' <= c && c <= '9');
        }

        void getInt(int &nr) {

            while(!digit(buffer[pos]))
                if(++pos == bufferDim)
                    getBuffer();
            nr = 0;
            while(digit(buffer[pos])) {

                nr = nr * 10 + buffer[pos] - '0';
                if(++pos == bufferDim)
                    getBuffer();
            }
        }
} cin;

struct neighbour {

    int vertex, cost;

    neighbour(const int &vertex, const int &cost) {

        this->vertex = vertex, this->cost = cost;
    }
};

struct edge {

    int a, b, cost;

    edge(const int &a, const int &b, const int &cost) {

        this->a = a, this->b = b, this->cost = cost;
    }

    bool operator <(const edge &other) const {

        return this->cost > other.cost;
    }
};
/****************end of class and struct declarations ****************/
priority_queue < edge > candidates;
vector < neighbour > G[MAX_N], A[MAX_N];

bool solved[MAX_N];
int father[MAX_N], depth[MAX_N], cost[MAX_N];
int n, m, k;


void readGraph() {

    cin.getBuffer();
    cin.getInt(n); cin.getInt(m); cin.getInt(k);

    for(int i = 1; i <= m; ++i) {

        int a, b, cost;
        cin.getInt(a); cin.getInt(b); cin.getInt(cost);
        G[a].pb(neighbour(b, cost));
        G[b].pb(neighbour(a, cost));
    }
}

void getAPM() {

    solved[1] = true;
    for(vector < neighbour >::iterator it = G[1].begin(); it != G[1].end(); ++it)
        candidates.push(edge(1, it->vertex, it->cost));

    int needed = n - 1;
    while(!candidates.empty() && needed) {

        edge Edge = candidates.top(); candidates.pop();

        if(!(solved[Edge.a] && solved[Edge.b])) {

            A[Edge.a].pb(neighbour(Edge.b, Edge.cost));
            A[Edge.b].pb(neighbour(Edge.a, Edge.cost));

            int newNode = (solved[Edge.a] == true) ? Edge.b : Edge.a;
            solved[newNode] = true;
            --needed;
            for(vector < neighbour >::iterator it = G[newNode].begin(); it != G[newNode].end(); ++it)
                if(!solved[it->vertex])
                    candidates.push(edge(newNode, it->vertex, it->cost));
        }
    }
    for(int i = 1; i <= n; ++i)
        solved[i] = false;
}

void dfs(int node) {

    solved[node] = true;
    for(vector < neighbour >::iterator it = A[node].begin(); it != A[node].end(); ++it)
        if(!solved[it->vertex]) {

            father[it->vertex] = node;
            depth[it->vertex] = depth[node] + 1;
            cost[it->vertex] = it->cost;
            dfs(it->vertex);
        }
}

int query(int x, int y) {

    int maxCost = 0;

    while(x != y) {

        if(depth[x] < depth[y])
            swap(x, y);
        maxCost = max(maxCost, cost[x]);
        x = father[x];
    }
    return maxCost;
}

int main() {

    readGraph();
    getAPM();
    for(int i = 1; i <= n; ++i)
        if(!solved[i])
            dfs(i);
    for(; k; --k) {

        int x, y;
        cin.getInt(x); cin.getInt(y);
        printf("%d\n", query(x, y));
    }
    return 0;
}