Cod sursa(job #2117787)

Utilizator BourucLiviuBouruc Petru Liviu BourucLiviu Data 29 ianuarie 2018 18:22:31
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <cstdio>
#include <cctype>
#include <vector>
#include <queue>
#include <bitset>

#define BUF_SIZE 4096
#define N 50005
#define inf 1e9

using namespace std;

FILE *f = fopen("distante.in", "r");
FILE *g = fopen("distante.out", "w");

char buf[BUF_SIZE];
int pos = BUF_SIZE;

inline char getChar()
{
    if(pos == BUF_SIZE)
    {
        fread(buf, 1, BUF_SIZE, f);
        pos = 0;
    }
    return buf[pos++];
}
inline int read()
{
    int result = 0;
    char c;
    do
    {
        c = getChar();
    } while(!isdigit(c));
    do
    {
        result = result * 10 + (c - '0');
        c = getChar();
    } while(isdigit(c));
    return result;
}

int n, s, D[N], dist[N];
vector <pair <int, int> > G[N];
priority_queue <pair <int, int> > heap;
bitset <N> viz;

void Dijkstra()
{
    for(int i = 1; i <= n; ++i) dist[i] = inf;
    dist[s] = 0;

    heap.push(make_pair(0, s));
    while(!heap.empty())
    {
        int nod = heap.top().second;
        heap.pop();

        if(viz[nod]) continue;
        viz[nod] = 1;
        for(int i = 0; i < G[nod].size(); ++i)
        {
            int to = G[nod][i].first;
            int cost = G[nod][i].second;
            if(dist[to] > dist[nod] + cost)
            {
                dist[to] = dist[nod] + cost;
                heap.push(make_pair(-dist[to], to));
            }
        }
    }
}

int main()
{
    int t, m;
    t = read();
    while(t--)
    {
        n = read(); m = read(); s = read();
        for(int i = 1; i <= n; ++i) D[i] = read();
        for(int i = 1, x, y, c; i <= m; ++i)
        {
            x = read(); y = read(); c = read();
            G[x].push_back(make_pair(y, c));
            G[y].push_back(make_pair(x, c));
        }
        Dijkstra();

        bool ok = 1;
        for(int i = 1; i <= n && ok; ++i)
            if(D[i] != dist[i]) ok = 0;
        if(ok) fprintf(g, "DA\n");
        else fprintf(g, "NU\n");

        for(int i = 1; i <= n; ++i) G[i].clear();
        viz.reset();
    }
    fclose(f); fclose(g);
    return 0;
}