Pagini recente » Cod sursa (job #3175744) | Cod sursa (job #2058665) | Cod sursa (job #1349421) | Cod sursa (job #539639) | Cod sursa (job #2964317)
#include <bits/stdc++.h>
#define N 50004
#define inf 1e9
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
int z;
vector< pair<int, int> > g[N];
priority_queue< pair<int, int>, vector< pair<int, int> > > q;
int D[N], S[N];
void Citire()
{
int i;
int x, y, c;
fin >> n >> m;
z = 1;
for( i=1; i<=m; i++ )
{
fin >> x >> y >> c;
g[x].push_back( {y, c} );
}
}
void Dijkstra( int x )
{
int i;
q.emplace( make_pair(0, x) );
D[x] = 0;
for( i=1; i<=n; i++ )
if( i != x ) D[i] = inf;
while( !q.empty() )
{
int nod = q.top().second;
int c = D[nod];
q.pop();
if( !S[nod] )
{
for( auto y : g[nod] )
{
if( c + y.second < D[y.first] )
{
D[y.first] = c + y.second;
//T[y.first] = nod;
q.emplace( make_pair( -D[y.first], y.first ) );
}
}
}
S[nod] = 1;
}
}
void Rezolvare()
{
int i;
for (i = 2; i <= n; i++)
if (D[i] == inf) fout << "0 ";
else fout << D[i] << " ";
}
int main()
{
Citire();
Dijkstra(z);
Rezolvare();
return 0;
}