Pagini recente » Cod sursa (job #2684886) | Cod sursa (job #2980494) | Cod sursa (job #458907) | Cod sursa (job #370701) | Cod sursa (job #1379594)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50010;
const int INF = 0x3f3f3f3f;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, d[MAXN];
vector <pair <int, int> > g[MAXN];
set <pair <int, int> > s;
void domnul_dijkstra()
{
for (int i = 2; i <= n; ++i)
d[i] = INF;
s.insert(make_pair(0, 1));
while (!s.empty())
{
int node = s.begin() -> second;
s.erase(s.begin());
for (vector <pair <int, int> > :: iterator it = g[node].begin(); it != g[node].end(); ++it)
{
int v = it -> first;
int c = it -> second;
if (d[v] > d[node] + c)
{
if (d[v] != INF) s.erase(make_pair(d[v], v));
d[v] = d[node] + c;
s.insert(make_pair(d[v], v));
}
}
}
for (int i = 2; i <= n; ++i)
{
if (d[i] != INF)
fout << d[i] << " ";
else
fout << 0 << " ";
}
}
int main()
{
fin >> n >> m;
for (int a, b, c; m; --m)
{
fin >> a >> b >> c;
g[a].push_back(make_pair(b, c));
}
domnul_dijkstra();
return 0;
}