Pagini recente » Cod sursa (job #3290334) | Cod sursa (job #1576415) | Cod sursa (job #40488) | Cod sursa (job #1733439) | Cod sursa (job #2918850)
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
ifstream fin ("distante.in");
ofstream fout ("distante.out");
const string msg[2] = {"NU", "DA"};
const int INF = 2e9;
const int MAX_N = 50005;
int n, m, nodSTART, a, b, c;
int input[MAX_N], dist[MAX_N];
bool same;
vector <pair<int, int>> e[MAX_N];
int crt, nxt, cst;
priority_queue<pair<int, int>> best;
bitset <MAX_N> viz;
void dijkstra(int start){
for(int i=1; i<=n; i++){
viz[i] = false;
dist[i] = INF;
}
dist[start] = 0;
best.push({dist[start], start});
while(!best.empty()){
crt = best.top().second;
best.pop();
if(!viz[crt])
for(int i=0; i < (int)e[crt].size(); i++){
nxt = e[crt][i].first;
cst = e[crt][i].second;
if(dist[nxt] > dist[crt] + cst){
dist[nxt] = dist[crt] + cst;
best.push({-dist[nxt], nxt});
}
}
viz[crt] = true;
}
}
int main (){
ios_base::sync_with_stdio(false);
fin.tie(nullptr), fout.tie(nullptr);
int teste;
fin>>teste;
while(teste--){
fin>>n>>m>>nodSTART;
for(int i=1; i<=n; i++){
fin>>input[i];
e[i].clear();
}
for(int i=1; i<=m; i++){
fin>>a>>b>>c;
e[a].push_back({b, c});
e[b].push_back({a, c});
}
dijkstra(nodSTART);
same = true;
for(int i=1; same && i<=n; i++)
if(input[i] != dist[i])
same = false;
fout<<msg[same]<<"\n";
}
return 0;
}