Cod sursa(job #1921278)

Utilizator deepsterescuCraciunescu Denis Bogdan deepsterescu Data 10 martie 2017 11:58:59
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vi;
const int INF = 1e9;
int n,m,d[50005],x,y,z,v,w,u;


int main()
{
    freopen("dijkstra.in","r",stdin);
    freopen("dijkstra.out","w",stdout);
   scanf("%d%d\n",&n,&m);
   vector<vi> adj(n+1);
   for (int i = 1; i<=n; ++i)
   {
       d[i] = INF;
   }
   for (int i = 0; i<m; ++i)
   {
       scanf("%d %d %d",&u,&v,&w);
       adj[u].push_back(ii(v,w));
   }
   priority_queue<ii, vector<ii>, greater<ii> > pq; pq.push(ii(0,1)); d[1] = 0;
   while (!pq.empty())
   {
       ii front = pq.top(); pq.pop();
       v = front.second; w = front.first;
       if (w > d[v]) continue;
       for (int i = 0; i<(int)adj[v].size(); ++i)
       {
           ii k = adj[v][i];
           if (d[v] + k.second < d[k.first])
           {
               d[k.first] = d[v] + k.second;
               pq.push(ii(d[k.first],k.first));
           }
       }
   }
   for (int i = 2; i<=n; ++i)
   {
       printf("%d ",d[i] == INF? 0:d[i]);
   }
}