Cod sursa(job #1245556)

Utilizator japjappedulapPotra Vlad japjappedulap Data 19 octombrie 2014 14:19:26
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <fstream>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;

#define NDIM 50001
#define MDIM 100001
#define INF 1<<28

#define BUFFER 1<<17
char Pars[BUFFER], *p;

int GET();
void Check();

ifstream is ("distante.in");
ofstream os ("distante.out");

int N, M, T, OG[NDIM], S, D[NDIM];
vector < pair <int,int> > v[NDIM];

struct COMP{
    bool operator ()(int a, int b)
    {   return D[a] > D[b];   }
};

priority_queue <int, vector<int>, COMP> Q;

void Solve();

int main()
{
    p = Pars, Check();
    for (T = GET(); T; --T) Solve();
    is.close();
    os.close();
};

void Solve()
{
    N = GET(); M = GET(); S = GET();
    for (int i = 1; i <= N; ++i)
        D[i] = INF;
    for (int i = 1; i <= N; ++i)
        OG[i] = GET();
    for (int i = 1, a, b, c; i <= M; ++i)
    {
        a = GET();
        b = GET();
        c = GET();
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }
    D[S] = 0;
    Q.push(S);
    for (int i, nod, cost; !Q.empty(); )
    {
        i = Q.top(); Q.pop();
        for (int j = 0; j < v[i].size(); ++j)
        {
            nod = v[i][j].first;
            cost = v[i][j].second;
            if (D[nod] > D[i] + cost)
            {
                D[nod] = D[i] + cost;
                Q.push(nod);
            }
        }
    }
    for (int i = 1; i <= N; ++i)
    {
        if (D[i] != OG[i])
        {
            os << "NU\n";
            break;
        }
        if (i == N && D[i] == OG[i])
            os << "DA\n";
    }
    for (int i = 1; i <= N; ++i)
        v[i].clear();
};

int GET()
{
    while (*p < '0' || *p > '9') ++p, Check();
    int X = 0;
    while (*p >= '0' && *p <= '9') X = X*10 + (*p-'0'), ++p, Check();
    return X;
};

void Check()
{
    if (*p == '\0') is.get(Pars, BUFFER, '\0'), p = Pars;
};