Cod sursa(job #1517979)

Utilizator AdrianGotcaAdrian Gotca AdrianGotca Data 5 noiembrie 2015 08:46:19
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <vector>
#include <queue>
#define INF 2147483647

using namespace std;

FILE *f=freopen("dijkstra.in","r",stdin);
FILE *g=freopen("dijkstra.out","w",stdout);

struct xyz{
    int poz,cost;
};
vector <xyz> G[50001];
queue <int> Q;
int n,m,viz[50001],viz1[50001];
void read();
void solve();
void write();
int main(){
    read();
    solve();
    write();
    return 0;
}

void read(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int x,y,c;
        xyz z;
        scanf("%d%d%d",&x,&y,&c);
        z.poz=y;
        z.cost=c;
        G[x].push_back(z);
    }
}

void solve(){
    Q.push(1);
    viz[1]=0;
    viz1[1]=1;
    for (int i=2;i<=n;i++){
        viz[i]=INF;
    }
    while (!Q.empty()){
        int x=Q.front();
        Q.pop();
        for (int i=0;i<G[x].size();i++){
            xyz y=G[x][i];
            if (!viz1[y.poz])
                viz[y.poz]=min(viz[y.poz],viz[x]+y.cost);
            Q.push(y.poz);
        }
        viz1[x]=1;
    }
}

void write(){
    for (int i=2;i<=n;i++){
        if (viz[i]==INF)
            printf("0 ");
        else {
            printf("%d ",viz[i]);
        }
    }
}