Cod sursa(job #2100912)

Utilizator abccnuCamelia Zalum abccnu Data 6 ianuarie 2018 15:54:43
Problema Ubuntzei Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

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

const int NMAX = 2000 + 5;
const int MMAX = 10000 + 5;
const int  oo = 1 << 30;

int N, M, K, c[NMAX];


int dp[NMAX][NMAX];

struct Elem
{
    int nod;
    int cost;

    bool operator<(const Elem &arg)const
    {
        return cost > arg.cost;
    }

    Elem (int _nod = 0, int _cost = 0)
    {
        nod = _nod;
        cost = _cost;
    }
};

priority_queue <Elem> q;
vector < Elem > gr[NMAX];

void read()
{
    int x, y, z;

    fin >> N >> M;
    fin >> K;
    for (int i = 1; i <= K; ++i)
        fin >> c[i];

    Elem aux;

    for(int i = 1; i <= M; ++i)
    {
        fin >> x >> y >> z;

        aux.nod = y;
        aux.cost = z;

        gr[x].push_back(aux);

        aux.nod = x;

        gr[y].push_back(aux);
    }
}

int Dijkstra(int poz, int x)
{
    Elem aux, alt;

    for (int i=1;i<=N;i++) dp[poz][i]=oo;

    dp[poz][x] = 0;

    alt.nod = x;
    alt.cost = 0;

    q.push(Elem(x, 0));

    while (!q.empty())
    {
        alt = q.top();
        q.pop();

        if (alt.cost == dp[poz][alt.nod])
        {
            for (vector <Elem> ::iterator it=gr[alt.nod].begin();it!=gr[alt.nod].end();it++)
            {
                aux = *it;
                if (dp[poz][aux.nod] > dp[poz][alt.nod] + aux.cost)
                {
                    dp[poz][aux.nod] = dp[poz][alt.nod] + aux.cost;
                    aux.cost = dp[poz][aux.nod];
                    q.push(Elem(aux.nod, aux.cost));

                }
            }
        }

    }


}


int main()
{
    read();
    Dijkstra(0, 1);
    //for (int i = 1; i <= K; ++i) Dijkstra(i, c[i]);

if (K == 0) fout << dp[0][N];
    return 0;
}