Pagini recente » Cod sursa (job #1801421) | Cod sursa (job #897249) | Cod sursa (job #1794144) | Cod sursa (job #989431) | Cod sursa (job #1887300)
#include <stdio.h>
#include <fstream>
#include<limits.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct edge
{
int nod;
int cost;
edge *next;
};
void add(edge **a, int cost, int from,int where)
{
edge * p = new edge();
p->cost = cost;
p->nod = where;
p->next = a[from];
a[from] = p;
}
struct mypair_comp{
bool operator()(pair<int,int> const& lhs, pair<int,int> const& rhs){
return lhs.first < rhs.first;
}
};
int djikstra(int x0, int *d, int *dist,int n, edge **a)
{
for(int i = 1; i <=n; ++i)
{
d[i] = INT_MAX;
}
std::priority_queue<pair<int,int>,vector<pair<int,int> >, mypair_comp > q;
q.push(make_pair(x0,0));
d[x0] = 0;
int viz[n+1];
for(int i = 0;i <= n;i++)
viz[i] =0;
while(q.empty() == false)
{
pair<int,int> pmin = q.top();
q.pop();
if(viz[pmin.first] == 1)
continue;
//int dmin = pmin.second;
int nmin = pmin.first;
edge *p = a[nmin];
while(p!= NULL)
{
if(d[p->nod] > d[nmin] + p->cost)
{
d[p->nod] = d[nmin] + p->cost;
q.push(make_pair(p->nod,d[p->nod]));
}
p = p->next;
}
}
for(int i = 1; i<= n ;i++)
{
//cout<<d[i]<<" ";
if(d[i] != dist[i])
return 0;
}
//cout<<"\n";
return 1;
}
void print_adj_list(edge **a,int n)
{
for(int i = 1; i<= n; ++i)
{
edge *p = a[i];
cout<<i<<": ";
while(p!= NULL)
{
cout<<p->nod<<" ";
p = p->next;
}
cout<<"\n";
}
}
int main(void)
{
int t;
int n,m,s;
std::ifstream f("distante.in");
f>>t;
std::ofstream g("distante.out");
for(int j = 0; j<t;++j)
{
f>>n>>m>>s;
int dist[n+1];
for(int i = 1;i <= n; ++i)
{
f>>dist[i];
}
edge *a[n+1];
for(int i = 0;i<=n;i++)
{
a[i] = NULL;
}
for(int i = 1;i<=m; ++i)
{
int x,y,z;
f>>x>>y>>z;
add(a,z,x,y);
}
int d[n+1];
if( djikstra(1,d,dist,n,a) )
g<<"DA\n";
else
g<<"NU\n";
}
return 0;
}