Cod sursa(job #2677554)

Utilizator FrostfireMagirescu Tudor Frostfire Data 26 noiembrie 2020 20:08:05
Problema PScNv Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.46 kb
//TLE cu cautare binara si apm
//poate merge Dijkstra csf
#include <cstdio>
#include <algorithm>
#include <ctype.h>
#include <vector>
#include <queue>
#define MMAX 500000
#define NMAX 250000

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& 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;
	}
};

const int inf = 2000000000;
int n, m, x, y, dist[NMAX+10];
bool viz[NMAX+10];
vector <pair <int, int> > nod[NMAX+10];
priority_queue <pair <int, int>, vector <pair <int, int> >, greater < pair <int, int> > > pq;

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({cost, nod2});
            nod[nod2].push_back({cost, nod1});
        }
    pq.push({0, x});
    for(int i=1; i<=n; i++) dist[i] = inf;
    dist[x] = 0;
    while(!pq.empty())
        {   pair <int, int> a = pq.top();
            pq.pop();
            if(a.second == y)
                {   printf("%d\n", a.first);
                    return 0;
                }
            if(viz[a.second]) continue;
            viz[a.second] = 1;
            for(int i=0; i<nod[a.second].size(); i++)
                if(!viz[nod[a.second][i].second])
                    {   int d = max(a.first, nod[a.second][i].first);
                        if(d < dist[nod[a.second][i].second])
                            {   dist[nod[a.second][i].second] = d;
                                pq.push({d, nod[a.second][i].second});
                            }
                    }
        }

    return 0;
}