Pagini recente » Cod sursa (job #2884974) | Cod sursa (job #1294517) | Cod sursa (job #1471090) | Cod sursa (job #81636) | Cod sursa (job #2475053)
#include <fstream>
#include <vector>
#include <queue>
#define cost first
#define nod second
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
typedef pair<int,int> Pair;
vector <Pair> G[50005];
priority_queue <Pair,vector<Pair>,greater<Pair>> h;
int n,m,x,y,c,i,d[50005];
bool sel[50005];
void dijkstra(int pl)
{
sel[pl]=true;
for(auto it : G[pl])
{
h.push(it);
}
while(!h.empty())
{
while(!h.empty() && sel[h.top().nod]==true)
h.pop();
if(h.empty())
return;
pair<int,int> k=h.top();
h.pop();
sel[k.nod]=true;
d[k.nod]=k.cost;
for(auto it : G[k.nod])
{
if(!sel[it.nod])
h.push({k.cost+it.cost,it.nod});
}
}
}
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y>>c;
G[x].push_back({c,y});
}
dijkstra(1);
for(i=2;i<=n;i++)
g<<d[i]<<' ';
return 0;
}