Cod sursa(job #1659755)

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

using namespace std;

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

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

string buffer;
string::iterator buffer_it;

void read_int_nn( int &x ) {
    for ( ; *buffer_it<'0' || *buffer_it>'9'; ++buffer_it ) ;
    for ( x= 0; *buffer_it>='0' && *buffer_it<='9'; ++buffer_it ) {
        x= x*10+*buffer_it-'0';
    }
}

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(  ) {
    getline ( fin, buffer, (char)0 );
    buffer_it= buffer.begin();

    int n, m, x, y;
    read_int_nn(n), read_int_nn(m), read_int_nn(x), read_int_nn(y);
    for ( int i= 1; i<=m; ++i ) {
        int a, b, c;
        read_int_nn(a), read_int_nn(b), read_int_nn(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;
}