Cod sursa(job #2677542)

Utilizator FrostfireMagirescu Tudor Frostfire Data 26 noiembrie 2020 19:41:37
Problema PScNv Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <cstdio>
#include <ctype.h>
#include <vector>
#include <queue>
#define NMAX 250000

using namespace std;

int n, m, x, y, maxi;
bool viz[NMAX+10];
vector <pair <int, int> > nod[NMAX+10];
queue <int> Q;

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& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long 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;
	}
};

int isOk(int val)
{   Q.push(x);
    for(int i=1; i<=n; i++) viz[i] = 0;
    viz[x] = 1;
    while(!Q.empty())
        {   int x = Q.front();
            Q.pop();
            if(x == y) return 1;
            for(int i=0; i<nod[x].size(); i++)
                if(!viz[nod[x][i].first] && nod[x][i].second <= val)
                    {   viz[nod[x][i].first] = 1;
                        Q.push(nod[x][i].first);
                    }
        }
    return 0;
}

int main()
{
    InParser f("pscnv.in");
    freopen("pscnv.out", "w", stdout);
    f >> n >> m >> x >> y;
    for(int i=1; i<=m; i++)
        {   int nod1, nod2, cost;
            f >> nod1 >> nod2 >> cost;
            nod[nod1].push_back({nod2, cost});
            nod[nod2].push_back({nod1, cost});
            maxi = max(maxi, cost);
        }
    int st = 1, dr = maxi, ans = 0;
    while(st <= dr)
        {   int mij = (st + dr) / 2;
            if(isOk(mij)) ans = mij, dr = mij - 1;
            else st = mij + 1;
        }
    printf("%d\n", ans);
    return 0;
}