Pagini recente » Cod sursa (job #256670) | Cod sursa (job #2858484) | Cod sursa (job #1876892) | Cod sursa (job #2023758) | Cod sursa (job #1799218)
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream fin("pscnv.in");
ofstream fout("pscnv.out");
const int dim = 250100;
const int inf = 1005;
int nodeCount, edgeCount, source, destination;
vector< pair<int, int> > graph[dim];
int dp[dim];
vector<int> lists[inf];
int Dijkstra() {
for (int i = 1; i <= nodeCount; ++i)
dp[i] = inf;
dp[source] = 0;
lists[0].push_back(source);
for (int i = 0; i < inf; ++i) {
for (auto it = lists[i].begin(); it != lists[i].end(); ++it) {
int curr = *it;
if (dp[curr] < i)
continue;
if (curr == destination)
return i;
for (auto adj : graph[curr]) {
int aux = max(dp[curr], adj.second);
if (dp[adj.first] <= aux)
continue;
dp[adj.first] = aux;
lists[aux].push_back(adj.first);
}
}
}
return inf;
}
int main() {
fin >> nodeCount >> edgeCount >> source >> destination;
for (int i = 1; i <= edgeCount; ++i) {
int x, y, cost;
fin >> x >> y >> cost;
graph[x].push_back(make_pair(y, cost));
}
fout << Dijkstra() << '\n';
return 0;
}
//Trust me, I'm the Doctor!