Cod sursa(job #2297557)

Utilizator GeorgianBaditaBadita Marin-Georgian GeorgianBadita Data 5 decembrie 2018 23:49:59
Problema PScNv Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 250005
#define INF 1e9
#define pb push_back
using namespace std;

ifstream in("pscnv.in");
ofstream out("pscnv.out");

struct Node{
private:
    int y, cost;
public:
    Node(const int& y, const int& cost): y{y}, cost{cost} {}

    int get_y() const {
        return this->y;
    }

    int get_cost() const {
        return this->cost;
    }

    bool operator<(const Node& other) const {
        return  this->cost > other.get_cost();
    }
};
vector<Node> G[NMAX];
int n, m, from, to;
queue<int> q;
bool viz[NMAX];

void read_data(){
    in >> n >> m >> from >> to;
    for(int i = 1; i<=m; i++){
        int x, y, c;
        in >> x >> y >> c;
        G[x].pb({y, c});
    }
}

void bfs_solve(int start, int dest){
    int k = 1;
    while(true){
        viz[start] = true;
        q.push(start);
        while(!q.empty()){
            int node = q.front();
            q.pop();
            for(const Node& nd : G[node]){
                if(nd.get_cost() <= k && !viz[nd.get_y()]){
                    if(nd.get_y() == dest){
                        out << k;
                        return;
                    }
                    q.push(nd.get_y());
                    viz[nd.get_y()] = true;
                }
            }
        }
        for(int i = 1; i<=n; i++){
            viz[i] = false;
        }
        k ++;
    }
}


int main(){
    read_data();
    bfs_solve(from, to);
    return 0;
}