Pagini recente » Cod sursa (job #2049691) | Cod sursa (job #2325352) | Cod sursa (job #85209) | Cod sursa (job #34701) | Cod sursa (job #2572886)
#include <bits/stdc++.h>
#define input "dijkstra.in"
#define output "dijkstra.out"
using namespace std;
typedef vector <int> VI;
const int nmax = 50001;
const int inf = (1<<30);
int n, m;
int d[nmax];
bool it[nmax];
vector <pair <int, int> > g[nmax];
class cmp
{
public:
bool operator()(int x, int y)
{
return d[x] > d[y];
}
};
priority_queue <int, VI, cmp> coada;
void dijk(int x)
{
for (int i = 1; i <= n; ++i)
d[i] = inf;
d[x] = 0;
coada.push(x);
it[x] = true;
while (!coada.empty())
{
int x = coada.top();
coada.pop();
it[x] = false;
if (it[x] == false)
{
for (auto i : g[x])
if (d[x] + i.second < d[i.first])
{
d[i.first] = d[x] + i.second;
coada.push(i.first);
}
it[x] = true;
}
}
}
main()
{
freopen(input, "rt", stdin);
freopen(output, "wt", stdout);
cin >> n >> m;
for (; m ; --m)
{
int x, y, z;
cin >> x >> y >> z;
g[x].push_back({y, z});
}
dijk(1);
for (int i = 2; i <= n; ++i)
if (d[i] == inf)
cout << 0 << " ";
else cout << d[i] << " ";
}