Cod sursa(job #1428792)

Utilizator Vally77FMI Calinescu Valentin Gelu Vally77 Data 5 mai 2015 08:49:15
Problema Distante Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream ka("distante.in");
ofstream ki("distante.out");

const int N_MAX = 50000;

bool vazut[N_MAX + 1];
int t, n, m, s, a, b, c, distanta[N_MAX + 1];

struct muchie
{
    int index, cost;
};

vector <muchie> graf[N_MAX + 1];
int coada[N_MAX + 1], curent;

string dijkstra()
{
    coada[++coada[0]] = s;
    curent = 1;
    while(curent <= coada[0])
    {
        int t = coada[curent];
        curent++;
        if(!vazut[t])
        {
            vazut[t] = true;
            for(int i = 0; i < graf[t].size(); i++)
            {
                if(distanta[t] + graf[t][i].cost == distanta[graf[t][i].index])
                    coada[++coada[0]] = graf[t][i].index;
            }
        }
    }
    for(int i = 1; i <= n; i++)
        if(!vazut[i])
            return "NU";
    return "DA";
}

int main()
{
    ka >> t;
    for(int i = 1; i <= t; i++)
    {
        ka >> n >> m >> s;
        for(int j = 1; j <= n; j++)
        {
            ka >> distanta[j];
            vazut[j] = false;
            graf[j].clear();
        }
        coada[0] = 0;
        for(int j = 1; j <= m; j++)
        {
            ka >> a >> b >> c;
            muchie mm;
            mm.index = b;
            mm.cost = c;
            graf[a].push_back(mm);
            mm.index = a;
            graf[b].push_back(mm);
        }
        ki << dijkstra() << '\n';
    }
}