Cod sursa(job #2358099)

Utilizator Cristian25Cristian Stanciu Cristian25 Data 27 februarie 2019 21:24:57
Problema Ubuntzei Scor 10
Compilator cpp-64 Status done
Runda simulare_oji_cls12_nr6 Marime 1.01 kb
#include <fstream>
#include <vector>
#include <queue>
#define dim 150000
#define pb push_back

using namespace std;

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

typedef pair<long long, long long> p;

vector<p> v[dim];
queue<long long> q;

long long N, M, K, cost[dim];

void bfs(long long nod)
{
    q.push(nod);
    while(!q.empty())
    {
        long long el = q.front();
        q.pop();
        for(long long i = 0; i < v[el].size(); ++i)
            if(cost[v[el][i].first] == -1 || cost[v[el][i].first] > cost[el] + v[el][i].second)
            {
                cost[v[el][i].first] = cost[el] + v[el][i].second;
                q.push(v[el][i].first);
            }
    }
}

int main()
{
    in >> N >> M >> K;
    for(long long i = 1; i <= M; ++i)
    {
        long long x, y, z;
        in >> x >> y >> z;
        v[x].pb({y, z});
    }
    cost[1] = 0;
    for(long long i = 2; i <= N; ++i)
        cost[i] = -1;
    bfs(1);
    out << cost[N];
    return 0;
}