Cod sursa(job #2126960)

Utilizator dragomirmanuelDragomir Manuel dragomirmanuel Data 10 februarie 2018 10:55:40
Problema Ubuntzei Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>

#define mp make_pair
#define pb push_back

using namespace std;

const int NodMax = 2005;
const int inf = 0x3f3f3f3f;
vector < pair < int, int > > G[NodMax]; ///nod si cost
int N, M, K;
int noduri[18];
int mask[32768+15];

void Read()
{
    scanf("%d %d", &N, &M);
    scanf("%d", &K);

    for(int i=1; i<=K; ++i)
        scanf("%d", &noduri[i]);

    for(int i=1; i<=M; ++i)
    {
        int nod1,nod2,cost;
        scanf("%d %d %d", &nod1, &nod2, &cost);
        G[nod1].pb(mp(cost,nod2));
        G[nod2].pb(mp(cost,nod1));
    }
}

priority_queue < pair < int, int > > Q;

void Dijkstra(int nod, int dist[])
{
    while(!Q.empty())
        Q.pop();

    for(int i=1; i<=N; ++i)
        dist[i] = inf;

    dist[nod] = 0;

    Q.push(mp(0,nod));

    while(!Q.empty())
    {
        int nod = Q.top().second;
        int cost = -Q.top().first;
        Q.pop();

        if(cost != dist[nod])
            continue;

        vector < pair < int, int > > ::iterator it;
        for(it=G[nod].begin(); it!=G[nod].end(); ++it)
        {
            if(dist[it->second] > cost + it->first)
            {
                dist[it->second] = cost + it->first;
                Q.push(mp(-dist[it->second],it->second));
            }
        }
    }
}

int main()
{
    freopen("ubuntzei.in", "r", stdin);
    freopen("ubuntzei.out", "w", stdout);

    Read();

    int dist[NodMax];

    Dijkstra(1,dist);

    cout << dist[N];

    return 0;
}