Cod sursa(job #4415)

Utilizator vlad_DVlad Dumitriu vlad_D Data 3 ianuarie 2007 04:03:50
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
struct nod{
	int c, id;
	nod(int _c, int _id) {c = _c; id = _id;}
	nod(){};
	};
struct muc{
	int to, c;
	muc(int _to, int _c) {to = _to; c = _c;};
	muc(){};
	};
bool operator<(const nod &a, const nod &b) {
	if (a.c > b.c) return true;
	return false;
	}
bool solve() {
	int i, j, n, m, s, viz;
	vector <int> target, cost;
	vector <vector <muc> > v;
	priority_queue<nod > Q;
	scanf("%d %d %d", &n, &m, &s);
	v.resize(n+2);
	target.push_back(0);
	cost.push_back(0);
	for (i=1; i<=n; i++) {
		int X;
		scanf("%d", &X);
		target.push_back(X);
		cost.push_back(999999);
	}
	for (i=0; i<m; i++) {
		int a, b, c;
		scanf("%d %d %d", &a, &b, &c);
		v[a].push_back(muc(b, c));
		v[b].push_back(muc(a, c));
	}
	viz = n;
	Q.push(nod(0, s));
	cost[s] = 0;
	while (Q.empty() == false) {
		nod top = Q.top(); Q.pop();
		if (cost[top.id] != target[top.id]) return false;
		viz--;
		if (viz == 0) break;
		for (i=0; i<v[top.id].size(); i++) 
			if (cost[top.id] + v[top.id][i].c < cost[v[top.id][i].to]) {
				cost[v[top.id][i].to] = cost[top.id] + v[top.id][i].c;
				Q.push(nod(cost[v[top.id][i].to], v[top.id][i].to));		
			}
	
	}
	if (viz) return false;
	return true;
	}
int main() {
	freopen("distante.in", "r", stdin);
	freopen("distante.out", "w", stdout);
	int t;
	scanf("%d", &t);
	while (t--) 
		if (solve()) printf("DA\n");
		else printf("NU\n");
	return 0;
}