Pagini recente » Cod sursa (job #2605191) | Cod sursa (job #2939384) | Cod sursa (job #2461742) | Cod sursa (job #2440393) | Cod sursa (job #3236848)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int nmax = 5e4, inf=1e9;
int n, m;
vector <pair<int, int>> graph[nmax+1];
vector <int> d(nmax+1, inf);
priority_queue <pair<int, int>> q;
bitset <nmax> vis;
void dijkstra()
{
q.push({0, 1});
d[1]=0;
while(!q.empty())
{
int nod=q.top().second;
q.pop();
if(vis[nod]==0)
{
for(int i=0; i<graph[nod].size(); i++)
{
int copil = graph[nod][i].first, dist = graph[nod][i].second;
if(d[nod]+dist<d[copil])
{
d[copil]=d[nod]+dist;
q.push({-d[copil], copil});
}
}
vis[nod]=1;
}
}
}
int main()
{
int a, b, c;
in>>n>>m;
for(int i=1; i<=m; i++)
{
in>>a>>b>>c;
graph[a].push_back({b, c});
}
dijkstra();
for(int i=2; i<=n; i++)
{
if(d[i]==inf)
out<<0<<" ";
else
out<<d[i]<<" ";
}
}