Pagini recente » Cod sursa (job #1924615) | Cod sursa (job #2065209) | Cod sursa (job #1019352) | Cod sursa (job #963426) | Cod sursa (job #2525733)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int NMAX = 100005;
const int INF = 0x3f3f3f;
int T, N, M, S, a, b, c;
int Dist[NMAX], InitialDist[NMAX];
struct compara {
bool operator() (int x, int y) {
return Dist[x] > Dist[y];
}
};
struct Edge {
int next;
int cost;
};
vector< Edge > G[NMAX];
bitset< NMAX > checked;
priority_queue< int, vector< int >, compara > Q;
void Dijkstra(int startNode)
{
for(int i = 0; i < N; ++i)
Dist[i] = INF;
Dist[startNode] = 0;
Q.push(startNode);
checked[startNode] = 1;
while(!Q.empty()) {
auto currentNode = Q.top();
Q.pop();
for(auto m : G[currentNode]) {
if(Dist[currentNode] + m.cost < Dist[m.next]) {
Dist[m.next] = Dist[currentNode] + m.cost;
if(!checked[m.next]) {
Q.push(m.next);
checked[m.next] = 1;
}
}
}
}
}
bool isMatch()
{
for(int i = 1; i <= N; i++)
if(Dist[i] != InitialDist[i])
return false;
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fin >> T;
for(int k = 0; k < T; ++k) {
fin >> N >> M >> S;
S--;
for(int i = 0; i < N; ++i)
fin >> InitialDist[i];
for(int i = 0; i < M; ++i) {
fin >> a >> b >> c;
G[a - 1].push_back({b - 1, c});
G[b - 1].push_back({a - 1, c});
}
Dijkstra(S);
(isMatch()) ? fout << "DA\n" : fout << "NU\n";
}
}