Cod sursa(job #2680217)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 2 decembrie 2020 22:53:00
Problema PScNv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <stdio.h>
#include <ctype.h>
using namespace std;
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;
	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};
InParser in ("pscnv.in");
ofstream out("pscnv.out");
const int nmax=25e4;
vector <pair<int, int> > muchii[nmax+2];
bitset <nmax+2> bt;
int n, m;
int x, y, z, ss, ff;
vector <int> coada[1002];
int main()
{
    in>>n>>m>>ss>>ff;
    for(int i=1; i<=m; i++){
        in>>x>>y>>z;
        muchii[x].push_back({y, z});
    }
    coada[0].push_back(ss);
    for(int i=0; i<=1000; i++){
        while(!coada[i].empty()){
            int elem=coada[i].back(); coada[i].pop_back();
            if(elem==ff){
                out<<i<<"\n";
                return 0;
            }
            if(bt.test(elem))
                continue;
            bt.set(elem);
            for(auto &x:muchii[elem])
                if(!bt.test(x.first))
                    coada[max(x.second, i)].push_back(x.first);
        }
    }
    return 0;
}