Cod sursa(job #1370328)

Utilizator span7aRazvan span7a Data 3 martie 2015 13:55:36
Problema Distante Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 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()) {
        int node = q.front();
        q.pop();
        viz[node] = 0;
        for(auto e : v[node]) {
            if(cost[e.inf] > cost[node] + e.c) {
                cost[e.inf] = cost[node] + e.c;
                if(!viz[e.inf]) {
                    viz[e.inf] = 1;
                    q.push(e.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';
            for(j=1;j<=m;j++){f>>a>>b>>co;}
                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;
}