Cod sursa(job #2205429)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 19 mai 2018 09:58:09
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream fin("distante.in");
ofstream fout("distante.out");

const int  Inf = 0x3f3f3f3f;

using PII = pair<int, int>;
using VI = vector<int>;
using VB = vector<bool>;
using VP = vector<PII>;
using VVP = vector<VP>;

VVP G;
VI d,dwrong;
int n,s;

void ReadGraph();
void Dijkstra(int S, VI& d);

int main()
{
	int t;
	fin >> t;
	for ( ; t > 0; --t) {
    ReadGraph();
    Dijkstra(s, d);
    bool nu = false;
	for (int i = 2; i <= n; ++i)
        if (d[i] != dwrong[i]) {
			fout << "NU\n";
			nu = true;
			break;
			}
    if(!nu)
      fout << "DA\n";
     }



    fin.close();
    fout.close();

    return 0;
}

void Dijkstra(int x, VI& d)
{
    priority_queue<PII, VP, greater<PII>> Q;
    d = VI(n + 1, Inf);
    d[x] = 0;
    Q.push({0, x});

    int y, w, dx;
    while (!Q.empty())
    {
        x = Q.top().second; dx = Q.top().first;
        Q.pop();
        if (d[x] < dx)
            continue;
        for (const PII& p : G[x])
        {
            y = p.first;
            w = p.second;
            if (d[y] > d[x] + w)
            {
                d[y] = d[x] + w;
                Q.push({d[y], y});
            }
        }

    }
}

void ReadGraph()
{
    int m;
    fin >> n >> m >> s;
    dwrong = VI ( n + 1);
    G = VVP(n + 1);
    for ( int i = 1; i <= n; ++i)
		fin >> dwrong[i];
    int x, y, w;
    while (m--)
    {
        fin >> x >> y >> w;
        G[x].push_back({y, w});
        G[y].push_back({x, w});
    }
}