Pagini recente » Cod sursa (job #1144552) | Cod sursa (job #1215208) | Cod sursa (job #1328558) | Cod sursa (job #185128) | Cod sursa (job #2228754)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
#define MAXN 100010
#define INF 0x3f3f3f3f
#define cout g
struct point
{
int nod;
int cost;
};
bool operator<(point a, point b)
{
return a.cost > b.cost;
}
vector< pair<int, int> > G[MAXN];
priority_queue< point > pq;
int dist[MAXN];
int main()
{
int n, m;
f >> n >> m;
for (int i = 0; i < m; ++i)
{
int x, y, c;
f >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
memset(dist, 0x3f, sizeof(dist));
pq.push({1, 0});
while (!pq.empty())
{
int nod = pq.top().nod;
int d = pq.top().cost;
pq.pop();
if (dist[nod] != INF) {
continue;
}
dist[nod] = d;
for (int i = 0; i < G[nod].size(); ++i)
{
pq.push({G[nod][i].first, d + G[nod][i].second});
}
}
for (int i = 2; i <= n; ++i)
{
if (dist[i] == INF) {
cout << 0 << ' ';
} else {
cout << dist[i] << ' ';
}
}
cout << endl;
return 0;
}