Pagini recente » Cod sursa (job #1861238) | Cod sursa (job #1923860) | Cod sursa (job #299374) | Cod sursa (job #1148150) | Cod sursa (job #2718339)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N = 50001;
const int INF = 1e9;
vector <pair <int, int>> a[N];
queue <int> q;
int n, d[N];
bool inq[N];
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int main()
{
int m;
in >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y, c;
in >> x >> y >> c;
a[x].push_back({y, c});
}
in.close();
//initializez d:
for (int i = 2; i <= n; i++)
{
d[i] = INF;
}
q.push(1);
d[1] = 0;
inq[1] = true;
while (!q.empty())
{
//scot din q:
int x = q.front();
q.pop();
inq[x] = false;
//parcurg succesorii lui x:
for (auto p: a[x])
{
int y = p.first;
int c = p.second;
if (d[x] + c < d[y])
{
d[y] = d[x] + c;
if (!inq[y])
{
q.push(y);
inq[y] = true;
}
}
}
}
for (int i = 2; i <= n; i++)
{
if (d[i] != INF)
{
out << d[i] << " ";
}
else
{
out << "0 ";
}
}
out.close();
return 0;
}