Cod sursa(job #2117703)

Utilizator BourucLiviuBouruc Petru Liviu BourucLiviu Data 29 ianuarie 2018 10:49:58
Problema Ubuntzei Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <fstream>
#include <vector>
#include <queue>

#define inf 1e9

using namespace std;

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

int n, k, v[2000], dist[2005];
vector <pair <int, int> > L[2005];
priority_queue <pair <int, int> > heap;
bool viz[2005];

void Dijkstra()
{
    for(int i = 2; i <= n; ++i) dist[i] = inf;

    heap.push(make_pair(0, 1));
    while(!heap.empty())
    {
        int nod = heap.top().second;
        heap.pop();

        if(viz[nod]) continue;
        viz[nod] = 1;
        for(int i = 0; i < L[nod].size(); ++i)
        {
            int to = L[nod][i].first;
            int cost = L[nod][i].second;
            if(dist[to] > dist[nod] + cost)
            {
                dist[to] = dist[nod] + cost;
                heap.push(make_pair(-dist[to], to));
            }
        }
    }
}

int main()
{
    int m;
    fin >> n >> m;
    fin >> k;
    for(int i = 1; i <= k; ++i) fin >> v[i];
    for(int i = 1, x, y, c; i <= m; ++i)
    {
        fin >> x >> y >> c;
        L[x].push_back(make_pair(y, c));
        L[y].push_back(make_pair(x, c));
    }
    fin.close();

    Dijkstra();

    fout << dist[n];
    fout.close();
    return 0;
}