Pagini recente » Profil janejones4237 | Cod sursa (job #1052208)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#define NMAX 50000
#define INF 0x3f3f3f3f
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
int T, N, M, S, dist[NMAX], dist_initiale[NMAX], marked[NMAX];
struct cmp{
bool operator() (pair <int, int> &a, pair <int, int> &b){
return a.second > b.second;
}
};
vector< pair<int, int> > vf[NMAX];
priority_queue <pair <int, int>, vector <pair <int, int> >, cmp> heap;
void disjkistra(){
int x, y, c;
while(!heap.empty()){
pair <int, int> top, other;
vector <pair <int, int> > ::iterator it;
top = heap.top();
marked[top.first] = true;
heap.pop();
x = top.first;
for(it = vf[x].begin(); it != vf[x].end(); it++){
y = it->first;
c = it->second;
if(dist[y] >= dist[x] + c){
dist[y] = dist[x] + c;
heap.push(make_pair(y, dist[y]));
}
}
}
}
void verifica(){
int i;
for(i = 1; i <= N; i++){
if(dist[i] != dist_initiale[i]){
out << "NU\n";
return;
}
}
out << "DA\n";
}
int main(){
int x, y, c, i, j;
in >> T;
for(i = 1; i <= T; i++){
in >> N >> M >> S;
for(int i = 1; i <= N; ++i){
dist[i] = INF;
vf[i].clear();
}
for(j = 1 ; j <= N; j++){
in >> dist_initiale[j];
}
for(j = 1; j <= M; j++){
in >> x >> y >> c;
vf[x].push_back(make_pair(y, c));
vf[y].push_back(make_pair(x, c));
}
dist[S] = 0;
heap.push(make_pair(S, 0));
disjkistra();
verifica();
}
}