Cod sursa(job #1659753)

Utilizator Athena99Anghel Anca Athena99 Data 22 martie 2016 16:11:43
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

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

const int inf= 1<<30;
const int nmax= 250000;

bool u[nmax+1];
int d[nmax+1];

struct str {
    int x, y;
};

struct str_cmp {
    bool operator () ( const str &x, const str &y ) {
        return x.y>y.y;
    }
};

vector <str> g[nmax+1];
priority_queue <str, vector <str>, str_cmp> q;

inline str mp( int x, int y ) {
    str sol;
    sol.x= x, sol.y= y;

    return sol;
}

void dijkstra( str x ) {
    d[x.x]= 0;
    for ( q.push(x); !q.empty(); ) {
        x= q.top();
        q.pop();
        u[x.x]= 0;

        for ( vector <str>::iterator it= g[x.x].begin(); it!=g[x.x].end(); ++it ) {
            if ( max(d[x.x], (*it).y)<d[(*it).x] ) {
                d[(*it).x]= max(d[x.x], (*it).y);
                if ( u[(*it).x]==0 ) {
                    u[(*it).x]= 1;
                    q.push(mp((*it).x, d[(*it).x]));
                }
            }
        }
    }
}

int main(  ) {
    int n, m, x, y;
    fin>>n>>m>>x>>y;
    for ( int i= 1; i<=m; ++i ) {
        int a, b, c;
        fin>>a>>b>>c;

        g[a].push_back(mp(b, c));
    }

    for ( int i= 1; i<=n; ++i ) {
        d[i]= inf;
    }
    dijkstra(mp(x, 0));

    fout<<d[y]<<"\n";

    return 0;
}