Cod sursa(job #1144762)

Utilizator andreiagAndrei Galusca andreiag Data 17 martie 2014 15:57:38
Problema Distante Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 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].clear();
        }
        G[s].push_back(make_pair(s, 0));
        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++) {
            int good = false;
            for (auto x: G[i]) {
                if (dist[x.first] > dist[i] + x.second) {
                    ok = false;
                }
                if (dist[i] == dist[x.first] + x.second)
                    good = true;
            }
            if (!good) ok = false;
        }

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

    return 0;
}