Pagini recente » Cod sursa (job #671522) | Cod sursa (job #2417938) | Cod sursa (job #2873135) | Cod sursa (job #760520) | Cod sursa (job #2529101)
#include <fstream>
#include <vector>
#include <iostream>
#include <bitset>
#define pii pair <int, int>
#include <stdlib.h>
using namespace std;
const int N = 250005, oo = 2e9;
int n, startnode, endnode, maxcost;
vector <pii> L[N];
bitset <N> seen;
void Read ()
{
ifstream fin ("pscnv.in");
int m, x, y, cost;
fin >> n >> m >> startnode >> endnode;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> cost;
L[x].push_back({y, cost});
L[y].push_back({x, cost});
maxcost = max(maxcost, cost);
}
}
bool OK;
bool DFS (int node, int cost)
{
seen[node] = 1;
for (auto next : L[node])
if (!seen[next.first] && next.second <= cost)
{
if (next.first == endnode)
OK = true;
else
DFS(next.first, cost);
}
return OK;
}
bool IsValidPath (int k)
{
seen.reset();
OK = false;
return (DFS(startnode, k));
}
int BinSearch ()
{
int left, right, mid, ans;
left = 1;
right = maxcost;
while (left <= right)
{
mid = (left + right) / 2;
if (IsValidPath(mid))
{
ans = mid;
right = mid - 1;
}
else left = mid + 1;
}
return ans;
}
void Solve ()
{
ofstream fout ("pscnv.out");
fout << BinSearch() << "\n";
fout.close();
}
int main()
{
Read();
Solve();
return 0;
}