Cod sursa(job #1507378)

Utilizator dnprxDan Pracsiu dnprx Data 21 octombrie 2015 17:17:51
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>
#define inFile "pscnv.in"
#define outFile "pscnv.out"

using namespace std;

struct Nod
{
    int nod, cost;
    bool operator < (const Nod& e) const
    {
        return cost > e.cost;
    }
};

vector <Nod> h[250002];
int n, d[250002], X, Y;
/// d[i]= costul minim al unei muchii de pe drumul de la
/// nodul de start X la nodul i

void Citire()
{
    int x, y, c, i, m;
    Nod w;
    ifstream fin(inFile);
    fin >> n >> m >> X >> Y;
    for (i = 1; i <= m; ++i)
    {
        fin >> x >> y >> c;
        w.nod = y;
        w.cost = c;
        h[x].push_back(w);
    }
    fin.close();
}

void Dijkstra()
{
    priority_queue<Nod> q;
    Nod w, w1;
    int i, k, c, cost;
    unsigned int j;
    for (i = 1; i <= n; i++)
        d[i] = 1000000000;
    d[X] = 0;
    w.nod = X;
    w.cost = 0;
    q.push(w);

    while (!q.empty())
    {
        w = q.top();
        q.pop();
        k = w.nod;
        for (j = 0; j < h[k].size(); j++)
        {
            i = h[k][j].nod;
            c = h[k][j].cost;
            cost = max(d[k], c);
            if (d[i] > cost)
            {
                d[i] = cost;
                w1.nod = i;
                w1.cost = cost;
                q.push(w1);
            }
        }
    }
}

void Afisare()
{
    ofstream fout(outFile);
    fout << d[Y] << "\n";
    fout.close();
}

int main()
{
    Citire();
    Dijkstra();
    Afisare();
    return 0;
}