Pagini recente » template/preoni-2006 | Istoria paginii runda/oni-2012-ziua2-11-12/clasament | Cod sursa (job #1899527) | Cod sursa (job #2906775) | Cod sursa (job #2806232)
#include <fstream>
#include <queue>
#include <vector>
#define N 50002
#define INF 2000000000
using namespace std;
int dc[N], dist[N];
struct bla
{
int no, co;
};
vector <bla> graph[N];
struct how
{
bool operator()(bla A, bla B)
{
return (A.co > B.co);
}
};
priority_queue <bla, vector <bla>, how> q;
bool viz[N];
void dijkstra(int nod)
{
int ve, d;
dist[nod] = 0;
q.push({ nod,0 });
while (!q.empty())
{
nod = q.top().no;
d = q.top().co;
q.pop();
if (!viz[nod])
{
for (int i = 0;i < graph[nod].size();++i)
{
ve = graph[nod][i].no;
if (dist[ve] > dist[nod] + graph[nod][i].co)
{
dist[ve] = dist[nod] + graph[nod][i].co;
q.push({ ve,dist[ve] });
}
}
viz[nod] = 1;
}
}
}
int main()
{
ifstream f("distante.in");
ofstream g("distante.out");
int t, n, m, st, x, y, c, ok;
f >> t;
while (t--)
{
f >> n >> m >> st;
ok = 1;
for (int i = 1;i <= n;++i) f >> dc[i], dist[i] = INF, viz[i] = 0, graph[i].clear();;
for (int i = 1;i <= m;++i)
{
f >> x >> y >> c;
graph[x].push_back({ y,c });
graph[y].push_back({ x,c });
}
dijkstra(st);
for (int i = 1;i <= n;++i)
if (dist[i] != dc[i])
{
g << "NU\n";
ok = 0;
break;
}
if (ok)
g << "DA\n";
}
f.close();
g.close();
return 0;
}