Cod sursa(job #1590369)

Utilizator mihai.constantinConstantin Mihai mihai.constantin Data 4 februarie 2016 22:41:40
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

const int N_max = 2005;
const int M_max = 10005;
const int INF = 2000000005;

int lst[N_max];
int vf[2 * M_max];
int urm[2 * M_max];
int cost[2 * M_max];
int nr;

bool viz[N_max];

vector <int> C;
int d[N_max];

int N, M, K;

void adauga(int x, int y, int c)
{
    //ADAUGA IN LISTA LUI x PE y

    nr++;
    vf[nr] = y;
    urm[nr] = lst[x];
    cost[nr] = c;
    lst[x] = nr;
}

int main()
{
    int i, nod, x, y, c, p, u, poz, COST;

    in >> N >> M;

    in >> K;
    for(i = 1; i <= K; i++)
    {
        in >> nod;

        viz[nod] = true;
    }

    for(i = 1; i <= M; i++)
    {
        in >> x >> y >> c;

        adauga(x, y, c);
        adauga(y, x, c);
    }

    for(i = 1; i <= N; i++) d[i] = INF;

    p = u = 0;

    //INSEREZ IN CODA NODUL 1
    C.push_back(1);
    d[1] = 0;

    while(p <= u)
    {
        x = C[p++];

        //PARCURG VECINII y AI LUI x

        poz = lst[x];

        while(poz != 0)
        {
            y = vf[poz];

            COST = cost[poz];

            if(d[y] > d[x] + COST)
            {
                u++;
                C.push_back(y);

                d[y] = d[x] + COST;
            }

            poz = urm[poz];
        }

    }

    for(i = 1; i <= N; i++) cout << d[i] << " ";


    return 0;
}