Cod sursa(job #1196918)

Utilizator apopeid15Apopei Daniel apopeid15 Data 9 iunie 2014 20:01:56
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <string.h>
#include <vector>
#include <queue>

using namespace std;
const int Nmax = 50005;
const int Inf = 0x3f3f3f3f;
typedef pair<int, int> pii;

int dist[Nmax];
int done[Nmax];

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

    int T, N, M, s, a, b, w;
    f >> T;
    for (int t = 0; t < T; t++) {
        f >> N >> M >> s;

        for (int i = 1; i <= N; i++) {
            f >> dist[i];
            done[i] = 0;
        }
        bool ok = (dist[s] == 0);
        done[s] = 1;

        for (int e = 0; e < M; e++) {
            f >> a >> b >> w;

            if (dist[a] + w < dist[b] || dist[b] + w < dist[a])
                ok = false;

            if (!done[a] && (dist[a] == dist[b] + w)) done[a] = 1;
            if (!done[b] && (dist[b] == dist[a] + w)) done[b] = 1;
        }

        for (int i = 1; i <= N && ok; i++)
            ok = (ok && done[i]);

        if (ok) g << "DA\n";
        else g << "NU\n";
    }

    return 0;
}