Pagini recente » Cod sursa (job #1712241) | Cod sursa (job #338121) | Cod sursa (job #770229) | Cod sursa (job #711172) | Cod sursa (job #2283407)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
int const NM = 1e5 / 2;
struct code {
int node , cost;
bool operator > (code r) const {
return cost > r . cost;
}
};
vector <code> v [1 + NM];
int dp [1 + NM] , dp2 [1 + NM];
priority_queue <code , vector <code> , greater <code> > q;
bool viz [1 + NM];
ifstream f ("distante.in");
ofstream g ("distante.out");
void shortest (int nodes , int last){
q . push ({last , 0});
while (! q . empty ()){
last = q . top () . node;
int cost = q . top () . cost;
viz [last] = true;
while (q . size () && viz [q . top () . node])
q . pop ();
for(auto i : v [last])
if (dp [i . node] > dp [last] + i . cost){
dp [i . node] = dp [last] + i . cost;
q . push ({i . node , dp [i . node]});
}
}
}
bool check (int n){
for(int i = 1 ; i <= n ; ++ i)
if (dp [i] != dp2 [i])
return false;
return true;
}
int main (){
int t , n , m , s;
f >> t;
while (t --){
f >> n >> m >> s;
fill (dp + 1 , dp + n + 1 , (1 << 30));
dp [s] = 0;
for(int i = 1 ; i <= n ; ++ i)
f >> dp2 [i];
for (int i = 1 ; i <= n ; ++ i)
v [i] . clear ();
for(int i = 1 ; i <= m ; ++ i){
int a , b , c;
f >> a >> b >> c;
v [a] . push_back ({b , c});
v [b] . push_back ({a , c});
}
fill (viz + 1 , viz + n + 1 , false);
shortest (n , s);
if (check (n))
g << "DA\n";
else
g << "NU\n";
}
return 0;
}