Cod sursa(job #1845869)

Utilizator valentinoMoldovan Rares valentino Data 11 ianuarie 2017 22:29:04
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
#define NM 50005
using namespace std;

ifstream f("distante.in");
ofstream g("distante.out");

int cost[NM], sol[NM],n, m, t, s;
vector < pair < int, int > > graf[NM];
queue < int > Q;

void Bellman_Ford(int k)
{
    int nod, nod2, drum;
    Q.push(k);
    for(int i = 1; i <= n; ++i) cost[i] = INF;
    cost[k] = 0;
    while(Q.empty() == false)
    {
        nod = Q.front();
        Q.pop();
        for(int i = 0; i < graf[nod].size(); ++i)
        {
            nod2 = graf[nod][i].first;
            drum = graf[nod][i].second;
            if(cost[nod2] > cost[nod] + drum)
            {
                cost[nod2] = cost[nod] + drum;
                Q.push(nod2);
            }
        }
    }
    for(int i = 1; i <= n; ++i)
    {
        if(sol[i] != cost[i])
        {
            g << "NU" << '\n';
            return;
        }
    }
    g << "DA" << '\n';
}

int main()
{
    int x, y, c;
    f >> t;
    for(int k = 1; k <= t; ++k)
    {
        f >> n >> m >> s;
        for(int i = 1; i<= n; ++i)
            graf[i].clear();
        for(int i = 1; i <= n; ++i)
            f >> sol[i];
        for(int i = 1; i <= m; ++i)
        {
            f >> x >> y >> c;
            graf[x].push_back(make_pair(y, c));
            graf[y].push_back(make_pair(x, c));
        }
        Bellman_Ford(s);
    }
}