Pagini recente » Cod sursa (job #2622742) | Cod sursa (job #380624) | Cod sursa (job #1630762) | Cod sursa (job #1454677) | Cod sursa (job #2630171)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int oo = INT_MAX;
vector<pair<int,int>> G[100005];
int n,m,d[100005];
bool sel[100005];
void Dijkstra(int pl)
{
priority_queue <pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> h;
for(int i=1;i<=n;i++)
{
d[i]=oo;
}
d[pl]=0;
for(auto it : G[pl])
{
d[it.second]=it.first;
h.push(it);
}
while(!h.empty())
{
while(!h.empty() && sel[h.top().second])
{
h.pop();
}
if(h.empty())
{
return;
}
pair<int,int> k=h.top();
h.pop();
sel[k.second]=true;
for(auto it : G[k.second])
{
if(k.first+it.first<d[it.second])
{
d[it.second]=k.first+it.first;
h.push({d[it.second],it.second});
}
}
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,c;
f>>x>>y>>c;
G[x].push_back({c,y});
}
Dijkstra(1);
for(int i=2;i<=n;i++)
{
g<<d[i]<<' ';
}
g<<'\n';
return 0;
}