Pagini recente » Cod sursa (job #1237268) | Cod sursa (job #879958) | Cod sursa (job #1951471) | Cod sursa (job #2405876) | Cod sursa (job #1736572)
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int nmx = 50020;
const int inf = 0x3f3f3f3f;
int n,m,start;
int c[nmx],dist[nmx];
vector <pair<int,int> > lv[nmx];
FILE *f=fopen("distante.in","r");
void golire( )
{
for(int i = 1; i < 50020; ++i)
{
dist[i] = inf;
lv[i].clear();
}
}
void citire()
{
fscanf(f,"%d%d%d", &n, &m, &start);
for(int i = 1; i <= n; ++i)
fscanf(f,"%d", &c[i]);
for(int i = 1; i <= m; ++i)
{
int x,y,z;
fscanf(f,"%d%d%d", &x, &y, &z);
lv[x].push_back(make_pair(y,z));
lv[y].push_back(make_pair(x,z));
}
}
priority_queue <pair<int,int> > q;
void dijkstra()
{
q.push(make_pair(0,start));
dist[start] = 0;
while(!q.empty())
{
int nod = q.top().second;
q.pop();
vector<pair<int,int> >::iterator it;
for(it = lv[nod].begin(); it != lv[nod].end(); ++it)
if(dist[it->first] > dist[nod] + it->second)
{
dist[it->first] = dist[nod] + it->second;
q.push(make_pair(-dist[it->first],it->first));
}
}
}
int main()
{
int nr;
fscanf(f,"%d", &nr);
FILE *f1=fopen("distante.out","w");
for(int i=1;i<=nr;i++)
{
golire();
citire();
dijkstra();
int ok = 1;
for(int i = 1; i <= n; ++i)
if(dist[i] != c[i])
ok = 0;
if(ok)
fprintf(f1,"DA\n");
else
fprintf(f1,"NU\n");
}
return 0;
}