Cod sursa(job #2529101)

Utilizator AndreiJJIordan Andrei AndreiJJ Data 22 ianuarie 2020 22:36:26
Problema PScNv Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#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;
}