Pagini recente » Cod sursa (job #2668233) | Cod sursa (job #567513) | Cod sursa (job #615300) | Cod sursa (job #2571265) | Cod sursa (job #1795393)
#include <cstdio>
#include <queue>
#define Pair pair<int,int>
using namespace std;
const int NM = 5e4+5;
const int inf = (1<<30);
int A,B,C, n,m, dist[NM], i, node;
vector< Pair > v[NM];
priority_queue< Pair, vector< Pair >, greater< Pair > > heap;
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d%d", &n, &m);
for(i=1; i<=m; ++i)
{
scanf("%d%d%d", &A, &B, &C);
v[A].push_back({B,C});
}
for(i=2; i<=n; ++i) dist[i] = inf;
heap.push({0,1});
while(heap.size())
{
node = heap.top().second;
heap.pop();
for(auto it : v[node])
if(dist[it.first] >= dist[node] + it.second)
{
dist[it.first] = dist[node] + it.second;
heap.push({ dist[it.first], it.first });
}
}
for(i=2; i<=n; ++i) printf("%d ", dist[i]==inf ? 0 : dist[i] );
return 0;
}