Cod sursa(job #748518)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 13 mai 2012 17:39:18
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#define INFI 0x3f3f3f3f

using namespace std;

typedef pair <int, int> pii;

bool vis[50001];
int n, m, sol[50001],d[50001];
vector <pii> g[50001];
priority_queue <pii, vector <pii>, greater<pii> > h;

int main()
{
	int i,j,a,b,c,x,y,t,s;
	vector <pii> :: iterator v;
	ifstream fin("distante.in");
	ofstream fout("distante.out");
    fin>>t;
	for (i=1;i<=t;++i)
	{
	    fin>>n>>m>>s;
	    for (j=1;j<=n;++j)
            fin>>d[j];
        for (j=1;j<=m;++j)
        {
            fin>>a>>b>>c;
            g[a].push_back(make_pair(c,b));
            g[b].push_back(make_pair(c,a));
        }
        memset(sol,0x3f,sizeof(sol));
        sol[s]=0;
        h.push(make_pair(0,s));
        while (!h.empty())
        {
            y=h.top().first;
            x=h.top().second;
            h.pop();
            vis[x]=1;
            for (v = g[x].begin();v!=g[x].end();++v)
                if (y+v->first<sol[v->second])
                {
                    sol[v->second]=y+v->first;
                    h.push(make_pair(sol[v->second],v->second));
                }
            while (!h.empty()&&vis[h.top().second])
                h.pop();
        }
        for (j=1;j<=n;++j)
        {
            g[j].clear();
            vis[j]=0;
        }
        for (j=1;j<=n;++j)
            if (sol[j]!=d[j])
            {
                fout<<"NU\n";
                break;
            }
        if (j!=n+1)
            continue;
        fout<<"DA\n";
	}
	return 0;
}