Cod sursa(job #3296923)

Utilizator claudiu.belciugBelciug Claudiu claudiu.belciug Data 18 mai 2025 19:06:35
Problema Distante Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 5.46 kb
// SPDX-License-Identifier: BSD-3-Clause

import java.io.*;
import java.util.*;

public class Main {
    static final long INF = Long.MAX_VALUE / 4;
    static int N, M, S;
    static int[] head, to, cost, nxt;
    static int edgeCnt;

    public static void main(String[] args) throws IOException {
        FastReader in = new FastReader("distante.in");
        BufferedWriter out = new BufferedWriter(new FileWriter("distante.out"));

        int T = in.nextInt();
        for (int t = 0; t < T; t++) {
            N = in.nextInt();
            M = in.nextInt();
            S = in.nextInt();

            long[] given = new long[N + 1];
            for (int i = 1; i <= N; i++) {
                given[i] = in.nextLong();
            }

            // build adjacency using static arrays
            head = new int[N + 1];
            Arrays.fill(head, -1);
            to = new int[2 * M];
            cost = new int[2 * M];
            nxt = new int[2 * M];
            edgeCnt = 0;
            for (int i = 0; i < M; i++) {
                int u = in.nextInt(), v = in.nextInt(), c = in.nextInt();
                addEdge(u, v, c);
                addEdge(v, u, c);
            }

            long[] dist = dijkstra();

            boolean ok = true;
            for (int i = 1; i <= N; i++) {
                long di = dist[i] >= INF / 2 ? -1 : dist[i];
                if (di != given[i]) {
                    ok = false;
                    break;
                }
            }

            out.write(ok ? "DA" : "NU");
            if (t < T - 1) out.newLine();
        }
        out.close();
    }

    static void addEdge(int u, int v, int w) {
        to[edgeCnt] = v;
        cost[edgeCnt] = w;
        nxt[edgeCnt] = head[u];
        head[u] = edgeCnt++;
    }

    static long[] dijkstra() {
        long[] dist = new long[N + 1];
        Arrays.fill(dist, INF);
        dist[S] = 0;

        int[] heap = new int[N + 1];
        int[] pos = new int[N + 1];
        int heapSize = 1;
        heap[1] = S;
        pos[S] = 1;

        while (heapSize > 0) {
            int u = heap[1];
            pos[u] = 0;
            heap[1] = heap[heapSize];
            pos[heap[1]] = 1;
            heapSize--;
            heapifyDown(heap, pos, dist, heapSize, 1);

            for (int e = head[u]; e != -1; e = nxt[e]) {
                int v = to[e];
                long nd = dist[u] + cost[e];
                if (nd < dist[v]) {
                    dist[v] = nd;
                    if (pos[v] == 0) {
                        heapSize++;
                        heap[heapSize] = v;
                        pos[v] = heapSize;
                        heapifyUp(heap, pos, dist, heapSize);
                    } else {
                        heapifyUp(heap, pos, dist, pos[v]);
                    }
                }
            }
        }
        return dist;
    }

    static void heapifyUp(int[] heap, int[] pos, long[] dist, int i) {
        while (i > 1) {
            int p = i >>> 1;
            if (dist[heap[p]] <= dist[heap[i]]) break;
            swap(heap, pos, p, i);
            i = p;
        }
    }

    static void heapifyDown(int[] heap, int[] pos, long[] dist, int heapSize, int i) {
        while (true) {
            int l = i << 1;
            int r = l | 1;
            int smallest = i;
            if (l <= heapSize && dist[heap[l]] < dist[heap[smallest]]) smallest = l;
            if (r <= heapSize && dist[heap[r]] < dist[heap[smallest]]) smallest = r;
            if (smallest == i) break;
            swap(heap, pos, i, smallest);
            i = smallest;
        }
    }

    static void swap(int[] heap, int[] pos, int i, int j) {
        int vi = heap[i], vj = heap[j];
        heap[i] = vj;
        heap[j] = vi;
        pos[vi] = j;
        pos[vj] = i;
    }

    static class FastReader {
        final int BUF_SIZE = 1 << 16;
        DataInputStream din;
        byte[] buffer;
        int ptr, len;

        FastReader(String file) throws IOException {
            din = new DataInputStream(new FileInputStream(file));
            buffer = new byte[BUF_SIZE];
            ptr = len = 0;
        }

        String next() throws IOException {
            int c;
            while ((c = read()) <= ' ') if (c == -1) return null;
            StringBuilder sb = new StringBuilder();
            do {
                sb.append((char) c);
            } while ((c = read()) > ' ');
            return sb.toString();
        }

        int nextInt() throws IOException {
            int c;
            while ((c = read()) <= ' ') if (c == -1) return -1;
            int x = 0;
            boolean neg = false;
            if (c == '-') {
                neg = true;
                c = read();
            }
            do {
                x = x * 10 + (c - '0');
            } while ((c = read()) >= '0' && c <= '9');
            return neg ? -x : x;
        }

        long nextLong() throws IOException {
            int c;
            while ((c = read()) <= ' ') if (c == -1) return -1;
            long x = 0;
            boolean neg = false;
            if (c == '-') {
                neg = true;
                c = read();
            }
            do {
                x = x * 10 + (c - '0');
            } while ((c = read()) >= '0' && c <= '9');
            return neg ? -x : x;
        }

        int read() throws IOException {
            if (ptr == len) {
                len = din.read(buffer);
                ptr = 0;
                if (len == -1) return -1;
            }
            return buffer[ptr++];
        }
    }
}