Pagini recente » Profil BlackLord | Cod sursa (job #576370) | Monitorul de evaluare | Statistici Boanta Catalin (bontac) | Cod sursa (job #2781514)
#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];
char s[101];
int ind;
int getnr() {
int nr;
nr = 0;
while (isdigit(s[ind])) {
nr = nr * 10 + (s[ind] - '0');
ind++;
}
ind++;
return nr;
}
void read() {
int i;
FILE *f;
f = fopen("pscnv.in", "r");
fgets(s, 101, f);
n = getnr(), m = getnr(), X = getnr(), Y = getnr();
i = 1;
while (!feof(f)) {
fgets(s, 101, f);
ind = 0;
input[i].x = getnr(), input[i].y = getnr(), input[i].p = getnr();
i++;
}
}
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;
}