Cod sursa(job #1813166)

Utilizator cella.florescuCella Florescu cella.florescu Data 22 noiembrie 2016 19:19:12
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5e4;
const int INF = 2e9;

struct Stuff {
  int node, cost;
};

vector <Stuff> g[MAXN + 1];
int d[MAXN + 1];

bool check(int node, int s) {
  if (node == s)
    return (d[node] == 0);
  int minim = INF;
  for (auto it : g[node])
    if (d[it.node] + it.cost < minim)
      minim = d[it.node] + it.cost;
  return (minim == d[node]);
}

int main()
{
    int t, n, m, s;
    ifstream fin("distante.in");
    fin >> t;
    ofstream fout("distante.out");
    for (t; t > 0; --t) {
      for (int i = 0; i <= MAXN; ++i)
        g[i].clear();
      fin >> n >> m >> s;
      for (int i = 1; i <= n; ++i)
        fin >> d[i];
      for (int i = 0; i < m; ++i) {
        int x, y, c;
        fin >> x >> y >> c;
        g[x].push_back({y, c});
        g[y].push_back({x, c});
      }
      for (n; n > 0 && check(n, s); --n) {}
      if (n <= 0)
        fout << "DA\n";
      else
        fout << "NU\n";
    }
    fin.close();
    fout.close();
    return 0;
}