Cod sursa(job #2030435)

Utilizator PondorastiAlex Turcanu Pondorasti Data 1 octombrie 2017 17:05:53
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 50000, INF = (1 << 30);
int d[NMAX + 2], teste, n = 0, m, s, a, b, c, dp[NMAX + 2], viz[NMAX + 5];
vector<pair<int, int>> g[NMAX + 2];
priority_queue<pair<int, int>> q;
void Solve() {
    
    for(int i = 1; i <= n; ++i)
        dp[i] = INF;
    
    dp[s] = 0;
    q.push(make_pair(s, 0));
    
    while (!q.empty()) {
        pair<int, int> front = q.top();
        q.pop();
        if(!viz[front.first]) {
            vector<pair<int, int>>::iterator it;
            for(it = g[front.first].begin(); it != g[front.first].end(); ++it) {
                if(dp[it -> first] > dp[front.first] + it -> second) {
                    dp[it -> first] = dp[front.first] + it -> second;
                    q.push(make_pair(it -> first, dp[it -> first]));
                }
            }
        }
        viz[front.first] = 1;
    }
}
int main() {
    ifstream cin("distante.in");
    ofstream cout("distante.out");
    cin >> teste;
    while (teste--) {
        for (int i = 1; i <= n; ++i) {
            while (!g[i].empty())   g[i].pop_back();
        }
        cin >> n >> m >> s;
        for (int i = 1; i <= n; i++)
            cin >> d[i];
        for (int i = 1; i <= m; ++i) {
            cin >> a >> b >> c;
            g[a].push_back(make_pair(b, c));
            g[b].push_back(make_pair(a, c));
        }
        Solve();
        bool ok = true;
        for(int i = 1; i <= n; i++) {
            if(dp[i] != d[i]) {
                ok = false;
                break;
            }
        }
        if(ok) cout << "DA\n";
        else cout << "NU\n";
    }
    return 0;
}