Pagini recente » Cod sursa (job #1373779) | Cod sursa (job #424545) | Cod sursa (job #37089) | Cod sursa (job #2023953) | Cod sursa (job #2760487)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
typedef long long ll;
const int NMAX = 5e4 + 5;
int n, m, x, y, c;
int D[NMAX];
bitset<NMAX> inq;
vector<pair<int, int>> graf[NMAX];
struct compare
{
bool operator()(int i, int j){
return D[i] > D[j];}
};
priority_queue<int, vector<int>, compare> q;
void Dijkstra(int startnode)
{
memset(D, 0x3f3f3f3f, sizeof(D));
D[startnode] = 0;
q.push(startnode);
inq[startnode] = true;
while (!q.empty())
{
int currNode = q.top();
q.pop();
inq[currNode] = false;
for (size_t i = 0; i < graf[currNode].size(); ++i)
{
if (D[currNode] + graf[currNode][i].second < D[graf[currNode][i].first])
{
D[graf[currNode][i].first] = D[currNode] + graf[currNode][i].second;
if (!inq[graf[currNode][i].first])
{
q.push(graf[currNode][i].first);
inq[graf[currNode][i].first] = true;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(0);
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y >> c;
graf[x].push_back({y, c});
}
Dijkstra(1);
for (int i = 2; i <= n; ++i){
if (D[i] == 0x3f3f3f3f) fout << "0 ";
else fout << D[i] << " ";
}
return 0;
}