Cod sursa(job #841843)

Utilizator stoicatheoFlirk Navok stoicatheo Data 25 decembrie 2012 09:56:55
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

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

const int N = 250010;

int n, m, x, y, sol[N], p;
char buff[100];
vector<pair<int, int> > v[N];
vector<int> l[1010];

inline int max(const int &a, const int &b) {
    return a > b ? a : b;
}

inline int ter() {
    int rez = 0;

    while(buff[p]>='0' && buff[p] <= '9')
        rez = rez * 10 + buff[p++] - '0';
    ++p;

    return rez;
}

int main() {
    int i, a, b, c, nod, j, cost, f;
    vector<pair<int, int> >::iterator it;

    in >> n >> m >> x >> y;
    in.get();

    for(i = 1; i<=m; ++i) {
        in.getline(buff, 100); p = 0;
        a = ter(); b = ter(); c = ter();

        v[a].push_back(pair<int, int>(b, c));
    }

    for(i = 1; i<=n; ++i)
        sol[i] = 1010;

    sol[x] = 0;
    l[0].push_back(x);

    for(i = 0; i<=1000; ++i)
        for(j = 0; j != l[i].size(); ++j) {
            nod = l[i][j];

            if(nod == y) {
                out << i << "\n";
                return 0;
            }

            if(sol[nod]!=i)
                continue;

            for(it = v[nod].begin(); it!=v[nod].end(); ++it) {
                cost = max(it->second, i);
                f = it->first;

                if(sol[f] > cost) {
                    sol[f] = cost;
                    l[cost].push_back(f);
                }
            }
        }

    return 0;
}