Pagini recente » Cod sursa (job #2745589) | Profil AntalKrisztian | Cod sursa (job #1455573) | Cod sursa (job #2363425) | Cod sursa (job #2602153)
#include <fstream>
#include <vector>
#include <set>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int N = 500001, Inf =1e9;
int n ,m ;
set <pair<int, pair<int,int>> >s;
int cost[N],viz[N];
int x , y ,z ;
vector <pair<int,int> > L[N];
void read();
void init();
void dijkstra();
void push(int x)
{
for(auto i : L[x])
{
if(cost[x] + i.second < cost[i.first])
cost[i.first] = cost[x] + i.second;
s.insert({i.second, {x, i.first}});
}
}
void write();
int main() {
read();
init();
dijkstra();
write();
return 0;
}
void read()
{
cin >> n >> m;
for(int i =1 ;i <=m ;i ++)
{
cin >> x >> y >> z;
L[x].push_back({y,z});
}
}
void init()
{
for(int i = 1 ;i <=n; i ++)
cost[i] = Inf;
cost[1] = 0;
viz[1] = 1;
push(1);
}
void dijkstra()
{
for(int i = 2 ;i <=n; i ++)
{
auto nod = s.begin();
while(true){
nod = s.begin();
if(viz[nod ->second.second])
s.erase(nod);
else
{
break;
}
}
viz[nod->second.second] =1;
push(nod ->second.second);
}
}
void write()
{
for(int i =2 ;i <=n; i ++)
cout << cost[i]<<" ";
}