Pagini recente » Cod sursa (job #3180565) | Cod sursa (job #248999) | Cod sursa (job #1155237) | Cod sursa (job #2160109) | Cod sursa (job #1460859)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50001
using namespace std;
int dist[NMAX];
class comp
{
public:
bool operator()(int a, int b)
{
return (dist[a] < dist[b]);
}
};
int calc[NMAX];
priority_queue <int, vector<int>, comp> q;
bool vis[NMAX];
vector <pair <int, int> > gr[10][NMAX];
bool check(int n)
{
for(int i = 1; i <= n; i++)
if(dist[i] != calc[i])
return false;
return true;
}
void minCosts(vector <pair <int, int> > gr[], int s, int n)
{
int i;
int inf = 100000000;
vector <pair <int, int> > :: iterator it;
for(i = 1; i <= n; i++)
dist[i] = inf;
dist[s] = 0;
q.push(s);
vis[s] = true;
while(!q.empty())
{
s = q.top();
q.pop();
for(it = gr[s].begin(); it != gr[s].end(); it++)
if(dist[it->first] > dist[s] + it->second)
{
dist[it->first] = dist[s] + it->second;
if(!vis[it->first])
{
vis[it->first] = true;
q.push(it->first);
}
}
vis[s] = false;
}
}
int main()
{
int n, m;
int i;
int t;
int s;
int a, b, c;
ifstream f("distante.in");
ofstream g("distante.out");
f >> t;
for(int ii = 0; ii < t; ii++)
{
f >> n >> m >> s;
for(i = 1; i <= n; i++)
f >> calc[i];
for(i = 0; i < m; i++)
{
f >> a >> b >> c;
gr[ii][a].push_back(make_pair(b, c));
gr[ii][b].push_back(make_pair(a, c));
}
minCosts(gr[ii], s, n);
if(check(n))
g << "DA\n";
else
g << "NU\n";
}
f.close();
g.close();
return 0;
}