Pagini recente » Cod sursa (job #1965501) | Cod sursa (job #492450) | Cod sursa (job #583080) | Cod sursa (job #2799726) | Cod sursa (job #1360646)
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#define inf 1<<30
#define maxn 500010
using namespace std;
int n, m;
vector<pair<int, int> > a[maxn];
priority_queue< pair<int, int> > pq;
bool inqueue[maxn];
int d[maxn];
void djikstra()
{
for (int i = 2; i <= n; i++)
d[i] = inf;
d[1] = 0;
inqueue[1] = true;
pq.push(make_pair(1, 0));
while (pq.size() > 0)
{
int nod = pq.top().first;
pq.pop();
for (int i = 0; i < a[nod].size(); i++)
{
int nodc = a[nod][i].first;
int costc = a[nod][i].second;
if (d[nodc] > d[nod] + costc)
{
d[nodc] = d[nod] + costc;
if (inqueue[nodc] == false)
{
pq.push(make_pair(nodc, d[nodc]));
inqueue[nodc] = true;
}
}
}
}
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d %d", &n, &m);
int x, y, z;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &x, &y, &z);
a[x].push_back(make_pair(y, z));
}
djikstra();
for (int i = 2; i <= n; i++)
{
printf("%d ", d[i]);
}
}