Pagini recente » Cod sursa (job #2300485) | Cod sursa (job #3149617) | Cod sursa (job #1994050) | Cod sursa (job #3143011) | Cod sursa (job #3125219)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
const int nmax = 1e5 + 3;
int n, m, x, y, c, d[nmax];
vector < pair <int, int> > v[nmax];
priority_queue < pair <int, int> > q;
int main()
{
f >> n >> m;
while (m--)
{
f >> x >> y >> c;
v[x].push_back(make_pair(y, c));
v[y].push_back(make_pair(x, c));
}
d[1] = 0;
for (int i = 2; i <= n; ++i)
d[i] = 1e9;
q.push(make_pair(0, 1));
while (!q.empty())
{
int nod = q.top().second;
q.pop();
for (int i = 0; i < v[nod].size(); ++i)
{
int urm = v[nod][i].first;
int cost = v[nod][i].second;
if (d[urm] > d[nod] + cost)
{
d[urm] = d[nod] + cost;
q.push(make_pair(-d[urm], urm));
}
}
}
for (int i = 2; i <= n; ++i)
{
if (d[i] != 1e9)
g << d[i] << ' ';
else g << 0 << ' ';
}
return 0;
}