Pagini recente » Cod sursa (job #2417227) | Cod sursa (job #2708984) | Cod sursa (job #1701272) | Cod sursa (job #1631262) | Cod sursa (job #2711629)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int sum, nrsol, cos[250005];
vector <pair <int, int> > e[50005];
priority_queue <pair <int, int>, vector<pair <int, int> >, greater<pair <int, int> > > nodes;
bitset <50005> vis;
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; ++i)
cos[i] = 1e9;
for(int i = 1; i <= m; ++i)
{
int a, b, c;
cin >> a >> b >> c;
e[a].push_back({c, b});
}
nodes.push({0, 1});
cos[1] = 0;
for(int w = 1; w < n; ++w)
{
int i = nodes.top().second;
vis[i] = true;
nodes.pop();
for(auto it = e[i].begin(); it != e[i].end(); ++it)
{
if(cos[it->second] > cos[i] + it->first && vis[it->second] == false)
{
cos[it->second] = cos[i] + it->first;
nodes.push({cos[it->second], it->second});
}
}
}
for(int i = 2; i <= n; ++i)
{
cout << cos[i] << ' ';
}
return 0;
}