Pagini recente » Cod sursa (job #578060) | Cod sursa (job #2921780) | Cod sursa (job #988971) | Cod sursa (job #2412799) | Cod sursa (job #2572862)
#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];
priority_queue <int, VI> 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;
for (auto i : g[x])
if (d[x] + i.second < d[i.first])
{
d[i.first] = d[x] + i.second;
if (it[i.first] == false)
{
coada.push(i.first);
it[i.first] = 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] << " ";
}