Pagini recente » Cod sursa (job #288809) | Cod sursa (job #2501775) | Cod sursa (job #1873069) | Cod sursa (job #1928140) | Cod sursa (job #3198677)
#include <fstream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct cmp {
bool operator() (pair<int, int> a, pair<int, int> b) const {
return a.second < b.second;
}
};
int n, m;
set<pair<int, int>, cmp> graf[50005];
queue<int> q;
bool viz[50005];
int dist[50005];
void bfs(int nod)
{
int cn;
viz[nod] = true;
q.push(nod);
while (!q.empty())
{
cn = q.front();
q.pop();
for (auto nextn : graf[cn])
{
if (viz[nextn.first] == false)
{
q.push(nextn.first);
viz[nextn.first] = true;
}
if (dist[nextn.first] == 0)
{
dist[nextn.first] = dist[cn] + nextn.second;
}
else
{
dist[nextn.first] = min(dist[cn] + nextn.second, dist[nextn.first]);
}
}
}
}
int main()
{
fin >> n >> m;
int x, y, z;
for (int i = 0; i < n; i++)
{
fin >> x >> y >> z;
graf[x].insert({ y, z });
}
bfs(1);
for (int i = 2; i <= n; i++)
{
fout << dist[i] << ' ';
}
return 0;
}