Pagini recente » Cod sursa (job #592700) | Cod sursa (job #743581) | Cod sursa (job #287349) | Cod sursa (job #579479) | Cod sursa (job #2529108)
#include <fstream>
#include <vector>
#include <iostream>
#include <bitset>
#include <stack>
#define pii pair <int, int>
#include <stdlib.h>
#include <set>
using namespace std;
const int N = 250005, oo = 2e9;
int n, startnode, endnode, maxcost;
set <pii> L[N];
set <pii> :: iterator it;
bitset <N> seen;
stack <int> st;
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].insert({cost, y});
L[y].insert({cost, x});
maxcost = max(maxcost, cost);
}
}
bool DFS (int node, int cost)
{
st.push(node);
while (!st.empty())
{
int k = st.top();
seen[k] = 1;
if (L[k].size())
{
bool valid = false;
for (it = L[k].begin(); it != L[k].end(); it++)
if (!seen[it -> second] && it -> first <= cost)
{
valid = true;
if (it -> second == endnode)
return true;
else st.push(it -> second);
}
if (!valid) st.pop();
}
else
st.pop();
}
return false;
}
bool IsValidPath (int k)
{
seen.reset();
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;
}