Pagini recente » Cod sursa (job #440659) | Cod sursa (job #562087) | Cod sursa (job #53251) | Cod sursa (job #2584222) | Cod sursa (job #846559)
Cod sursa(job #846559)
#include <vector>
#include <cstring>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
#define ushort int
const int maxn = 50005;
int found[maxn];
int best[maxn];
struct step
{
ushort nod,cost;
};
class heap
{
public:
step d[maxn];
int size;
heap()
{
size = 0;
memset(d,0,sizeof(d));
}
void push(step a)
{
d[++size] = a;
up_heap(size);
}
step pop()
{
step ret = d[1];
swap(d[1],d[size--]);
down_heap(1);
return ret;
}
void up_heap(int nod)
{
int tata;
while(nod > 1)
{
tata = (nod >> 1);
if( d[tata].cost > d[nod].cost)
{
swap(d[tata],d[nod]);
nod = tata;
}
else
return ;
}
}
void down_heap(int nod)
{
int fiu;
while( (nod << 1) <= this->size)
{
fiu = nod;
if(d[nod << 1].cost < d[fiu].cost)
fiu = nod << 1;
if( (nod << 1 ) + 1 <= this->size && d[ (nod << 1 ) + 1].cost < d[ fiu ].cost)
fiu = (nod << 1) + 1;
if(fiu != nod)
{
swap(d[fiu],d[nod]);
nod = fiu;
}
else
return ;
}
}
bool empty()
{
return !size;
}
} q;
vector<step>graf[maxn];
void dijkstra()
{
int i;
step cr;
while(!q.empty())
{
cr = q.pop();
for( i = graf[cr.nod].size() - 1 ; i >= 0 ; -- i)
{
if(cr.cost + graf[cr.nod][i].cost < best[graf[cr.nod][i].nod] || !best[graf[cr.nod][i].nod])
{
best[graf[cr.nod][i].nod] = cr.cost + graf[cr.nod][i].cost;
q.push((step){graf[cr.nod][i].nod,best[graf[cr.nod][i].nod]});
}
}
}
}
int main()
{
int t,n,m,s,i;
int a,b,c;
bool ok;
in >> t;
while(t--)
{
in >> n >> m >> s;
memset(best,0,n+1);
for(i = 1 ; i <= n ; ++ i)
{
graf[i].clear();
in >> found [ i ];
}
if(found[s] != 0)
{
printf("NU\n");
continue ;
}
for(i = 1 ; i <= m ; ++ i )
{
in >> a >> b >> c;
graf[a].push_back((step){b,c});
}
best[s] = 1;
q.push((step){s,1});
dijkstra();
ok = 1;
for(i = 1 ; i <= n && ok ; ++ i)
{
best[i] = best[i] ? best[i] - 1 : 0;
if(best[i] != found[i])
ok = 0;
}
if(ok)
out << "DA\n";
else
out << "NU\n";
}
return 0;
}