Pagini recente » Cod sursa (job #1449630) | Cod sursa (job #2029773) | Cod sursa (job #1217957) | Cod sursa (job #254312) | Cod sursa (job #2366424)
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#define MAX_INT 99999
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int T;
int N, M, S;
void solve(int N, int M, int S)
{
S--;
vector<int> givenDist;
vector<int> dist(N, MAX_INT);
vector< pair<int, int> > emptyVec;
vector< vector< pair<int, int> > > graph;
for(int i = 0; i < N; i++){
graph.push_back(emptyVec);
int val;
f >> val;
givenDist.push_back(val);
}
for(int i = 0; i < M; i++)
{
int weight, from, to;
f >> from >> to >> weight;
from--, to--;
graph[from].push_back(make_pair(to, weight));
graph[to].push_back(make_pair(from, weight));
}
set< pair<int,int> > minSet;
minSet.insert(make_pair(0, S));
dist[S] = 0;
while(!minSet.empty()){
pair<int,int> temporary = *(minSet.begin());
minSet.erase(minSet.begin());
int currentNode = temporary.second;
for(int i = 0; i < graph[currentNode].size(); i++){
int node = graph[currentNode][i].first;
int weight = graph[currentNode][i].second;
if(dist[node] > dist[currentNode] + weight){
if(dist[node] == MAX_INT)
minSet.erase(make_pair(dist[node], node));
dist[node] = dist[currentNode] + weight;
minSet.insert(make_pair(dist[node], node));
}
}
}
if(givenDist == dist)
g << "DA" << endl;
else
g << "NU" << endl;
}
int main()
{
f >> T;
for(int i = 0; i < T; i++){
f >> N >> M >> S;
solve(N,M,S);
}
}