Cod sursa(job #1370300)

Utilizator span7aRazvan span7a Data 3 martie 2015 13:49:28
Problema Distante Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include<fstream>
#include<queue>
#include<vector>
#define maxN 50001
#define M 1<<30
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
struct muc{
int inf,c;
};
vector<muc> v[maxN];
queue<int>q;
bool viz[maxN];
int cost[maxN],cost_sol[maxN];
int n,m;
void dijkstra(int sursa)
{
    int cur;
    fill(viz+1,viz+n+1,false);

    for(int i=1;i<=n;i++)
        cost[i]=M;
    cost[sursa]=0;
    q.push(sursa);
    viz[sursa]=true;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        viz[cur]=false;
        for(int j=0;j<v[cur].size();j++)

            if(cost[v[cur][j].inf]>cost[cur]+v[cur][j].c)
            {
                cost[v[cur][j].inf]=cost[cur]+v[cur][j].c;
                /*if(cost[v[cur][j].inf] < cost_sol[v[cur][j].inf])
                  {
                      g<<"NU"<<'\n';
                      return;
                  }
                */
                 if(viz[v[cur][j].inf]==false)
                {
                    viz[v[cur][j].inf]=true;
                    q.push(v[cur][j].inf);
                }
            }

    }
    for(int i=1;i<=n;i++)
        if(cost_sol[i] != cost[i] )
    {
        g<<"NU"<<'\n';
        return;
    }
    g<<"DA"<<'\n';

}
void citire()
{
    muc aux;
    int t,a,b,co,s,i,j;
    f>>t;
    while(t--)
    {
        for(j=1;j<=n;j++)
            v[j].clear();

        f>>n>>m>>s;

        for(j=1;j<=n;j++)
            f>>cost_sol[j];
        if(cost_sol[s]){
                g<<"NU"<<'\n';
                continue;
        }
        for(j=1;j<=m;j++)
        {
            f>>a>>b>>co;
            aux.inf=a;
            aux.c=co;
            v[b].push_back(aux);
            aux.inf=b;
            v[a].push_back(aux);
        }
        dijkstra(s);
    }


}
int main()
{
    citire();
    return 0;
}