Pagini recente » Cod sursa (job #2488906) | Cod sursa (job #2590986) | Cod sursa (job #2043514) | Cod sursa (job #1946475) | Cod sursa (job #2711659)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int sum, nrsol, cos[50005], b[250005], c[250005];
vector <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] = 1000000137;
for(int i = 1; i <= m; ++i)
{
int a;
cin >> a >> b[i] >> c[i];
e[a].push_back(i);
}
nodes.push({0, 1});
cos[1] = 0;
while (!nodes.empty())
{
int i = nodes.top().second;
nodes.pop();
for(auto it = e[i].begin(); it != e[i].end(); ++it)
{
if(cos[b[*it]] > cos[i] + c[*it])
{
cos[b[*it]] = cos[i] + c[*it];
nodes.push({cos[b[*it]], b[*it]});
}
}
}
for(int i = 2; i <= n; ++i)
{
if(cos[i] == 1e9 + 137)
cos[i] = 0;
cout << cos[i] << ' ';
}
return 0;
}