Cod sursa(job #1144769)

Utilizator andreiagAndrei Galusca andreiag Data 17 martie 2014 16:05:35
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 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;

vector<pii> G[Nmax];
int dist[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;
        s--;
        for (int i = 0; i < N; i++) {
            f >> dist[i];
            G[i] = vector<pii>();
        }
        for (int e = 0; e < M; e++) {
            f >> a >> b >> w;
            G[a-1].push_back(make_pair(b-1, w));
            G[b-1].push_back(make_pair(a-1, w));
        }
        
        bool ok = (dist[s] == 0);
        
        for (int i = 0; i < N && ok; i++) {
            if (i == s) continue;
            int good = false;
            for (auto x: G[i]) {
                if (dist[i] > dist[x.first] + x.second)
                    { ok = false; break; }
                else if (dist[i] == dist[x.first] + x.second)
                        { good = true; break; }
            }
            if (!good) ok = false;
        }

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

    return 0;
}