Cod sursa(job #1349396)

Utilizator andreey_047Andrei Maxim andreey_047 Data 20 februarie 2015 10:32:27
Problema Distante Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#define INF 999999
#define nmax 50005
using namespace std;
int t,n,m,s;
struct nod{
 int v,cost;
};
struct Coord{
 int x,c;
 bool operator <(const Coord &e) const{
  return c < e.c;
 }
};
inline void atac_brut(){
    int i;
    vector<nod>G[nmax];
    priority_queue<Coord>q;
    int x,y,c,ok[nmax],dp[nmax],ci[nmax];
    for(i=1;i<=n;i++)
        {scanf("%d",&ci[i]);ok[i]=0;dp[i]=INF;}
    nod w;
        while(m--){
         scanf("%d%d%d",&x,&y,&c);
            w.v=y;w.cost=c;
          G[x].push_back(w);
          w.v=x;
          G[y].push_back(w);
        }
    Coord b;
    b.x=s;b.c=0;
    q.push(b);ok[s]=1;dp[s]=0;
    while(!q.empty()){
        b=q.top();
        ok[b.x]=0;
        q.pop();
        for(i=0;i<G[b.x].size();i++){
            if(dp[b.x]+G[b.x][i].cost < dp[G[b.x][i].v]){
               dp[G[b.x][i].v] = dp[b.x]+G[b.x][i].cost;
               if(!ok[G[b.x][i].v]){
                ok[G[b.x][i].v]=1;
                Coord a;
                a.x=G[b.x][i].v;
                a.c=dp[G[b.x][i].v];
                q.push(a);
               }
            }
        }
    }
  for(i=1;i<=n;i++)
    if(dp[i]!=ci[i]){cout<<"NU\n";return;}
    cout<<"DA\n";
}
int main(){
    freopen("distante.in","r",stdin);
    freopen("distante.out","w",stdout);
    scanf("%d",&t);

    while(t--){
        scanf("%d%d%d",&n,&m,&s);
        atac_brut();
    }
    return 0;
}