Pagini recente » Cod sursa (job #1767384) | Cod sursa (job #2359210) | Cod sursa (job #257279) | Cod sursa (job #1636139) | Cod sursa (job #3257350)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#define pair pair<int,int>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
const int Max_n = 5e4 + 1;
const int Inf = 1 << 30;
int n, m;
vector<pair> graph[Max_n];
vector<int> d(Max_n, Inf);
bitset<Max_n> v;
priority_queue<pair, vector<pair>, greater<pair>> q;
void dijkstra(int node)
{
q.push({0, node});
d[node] = 0;
while(!q.empty())
{
node = q.top().second;
q.pop();
if(v[node])
continue;
v[node] = 1;
for(auto i: graph[node])
{
int neighbour = i.first;
int cost = i.second;
if(d[node] + cost < d[neighbour])
{
d[neighbour] = d[node] + cost;
q.push({d[neighbour], neighbour});
}
}
}
}
int main()
{
int x, y, z;
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
fin >> x >> y >> z;
graph[x].push_back({y, z});
}
dijkstra(1);
for(int i = 2; i <= n; ++i)
if(d[i] != Inf)
fout << d[i] << " ";
else
fout << "0 ";
fin.close();
fout.close();
return 0;
}