Pagini recente » Cod sursa (job #930938) | Cod sursa (job #69247) | Cod sursa (job #411375) | Cod sursa (job #1290409) | Cod sursa (job #2576764)
#include <bits/stdc++.h>
using namespace std;
const int dim = 50010;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m, dist[dim];
vector<pair<int, int> > muchii[dim];
void read()
{
in>>n>>m;
int x, y, c;
for(int i = 1; i <= m; i++)
{
in>>x>>y>>c;
muchii[x].push_back({y, c});
}
}
struct cmp
{
bool operator() (const int& a, const int& b) const{
return dist[a] < dist[b];
}
};
void dijsktra()
{
set<int> s;
int nod;
dist[1] = 0;
s.insert(1);
while(!s.empty())
{
nod = *(s.begin());
s.erase(s.begin());
for(auto it : muchii[nod])
{
if(dist[it.first] > dist[nod] + it.second || dist[it.first] == 0)
{
dist[it.first] = dist[nod] + it.second;
s.insert(it.first);
}
}
}
}
void print()
{
for(int i = 2; i <= n; i++)
out<<dist[i]<<' ';
}
int main()
{
read();
dijsktra();
print();
return 0;
}