Pagini recente » Cod sursa (job #249539) | Cod sursa (job #2260848) | Cod sursa (job #553372) | Cod sursa (job #1652671) | Cod sursa (job #2834467)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int N = 50050;
const int oo = INT_MAX;
int n, m, viz[N], d[N], zaharel[N];
vector<pair<int, int>> adj[N];
priority_queue<pair<int, int>> q;
void Dijkstra(int source)
{
for (int i = 1; i <= n; i++)
if (i != source)
d[i] = oo;
else
d[i] = 0;
q.push(make_pair(0,source));
while (!q.empty())
{
int k = q.top().second;
q.pop();
if (viz[k] == 0)
{
viz[k] = 1;
for (auto it : adj[k])
{
int cost = it.first;
int i = it.second;
if (d[i] > d[k] + cost)
{
d[i] = d[k] + cost;
q.push(make_pair(-d[i], i));
}
}
}
}
for (int i = 1; i <= n; i++)
if (d[i] == oo)
d[i] = 0;
/*
for (int i = 1; i <= n; i++)
cout << d[i] << " ";
cout << '\n';
*/
}
string ver(int a[], int b[])
{
// cout << "N este egal cu: " << n << '\n';
for (int i = 1; i <= n; i++)
if (a[i] != b[i])
return "NU\n";
return "DA\n";
}
int main()
{
int probleme;
fin >> probleme;
for (; probleme != 0; probleme--)
{
int x, y, cost, plecare;
fin >> n >> m >> plecare;
for (int i = 1; i <= n; i++)
fin >> zaharel[i];
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> cost;
adj[y].push_back({cost, x});
adj[x].push_back({cost, y});
}
for (int i = 1; i <= n; i++)
viz[i] = d[i] = 0;
Dijkstra(plecare);
fout << ver(d, zaharel);
}
/*Dijkstra(1);
for (int i = 1; i <= n; i++)
if (d[i] == oo)
fout << "0 ";
else
fout << d[i] << " ";
*/
return 0;
}