Cod sursa(job #2781506)

Utilizator DragosC1Dragos DragosC1 Data 9 octombrie 2021 17:59:39
Problema PScNv Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
 
int n, m, X, Y;
vector<pair<int, int>> a[250001];
 
struct dt {
    int x, y, p;
};
 
dt input[500001], input2[500001];
 
void read() {
    int i;
    ifstream f("pscnv.in");
    f >> n >> m >> X >> Y;
    for (i = 1; i <= m; ++i)
        f >> input[i].x >> input[i].y >> input[i].p;
    f.close();
}
 
int rez;
 
queue<int> Q;
bool viz[250001];
 
bool ok(int pond) {
    int i, x;
    for (i = 1; i <= n; ++i)
        viz[i] = 0;
    Q.push(X);
    viz[X] = 1;
    while (!Q.empty()) {
        x = Q.front();
        Q.pop();
        for (i = 0; i < a[x].size(); i++)
            if (a[x][i].first <= pond && !viz[a[x][i].second]) {
                Q.push(a[x][i].second);
                viz[a[x][i].second] = 1;
            }
    }
    return viz[Y];
}
 
void normalize() {
    int i, cm = 0;
    input2[++cm] = input[1];
    for (i = 2; i <= m; ++i)
        if (input[i].x != input[i - 1].x || input[i].y != input[i - 1].y)
            input2[++cm] = input[i];
    m = cm;
}
 
void construct_graph() {
    int i, x, y, p;
    for (i = 1; i <= m; ++i) {
        x = input2[i].x, y = input2[i].y, p = input2[i].p;
        a[x].emplace_back(p, y);
    }
}
 
bool csort(dt a, dt b) {
    if (a.x < b.x)
        return 1;
    else if (a.x == b.x && a.y < b.y)
        return 1;
    else if (a.x == b.x && a.y == b.y && a.p < b.p)
        return 1;
    return 0;
}
 
void solve() {
    sort(input + 1, input + m + 1, csort);
 
    normalize();
    construct_graph();
 
    int st, dr, mij;
    st = 1, dr = 1000;
    while (st <= dr) {
        mij = (st + dr) / 2;
        if (ok(mij)) {
            rez = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }
}
 
void output() {
    ofstream g("pscnv.out");
    g << rez;
    g.close();
}
 
int main() {
    read();
    solve();
    output();
    return 0;
}