Pagini recente » Cod sursa (job #2498508) | Cod sursa (job #2160159) | Cod sursa (job #1097650) | Cod sursa (job #2479633) | Cod sursa (job #1991897)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int N = 50001;
const int INF = 2000001000;
int d[N],pred[N],m,n,c;
priority_queue <pair <int,int> > h;
vector <pair <int,int> > a[N];
bool sel[N];
void citire()
{
fin >> n >> m;
int x,y;
for(int i=1;i<=m;i++)
{
fin >> x >> y >> c;
a[x].push_back(make_pair(y,c));
}
}
void dijkstra(int x0)
{
int x,y;
for(int i=1;i<=n;i++)
{
d[i]=INF;
}
d[x0]=0;
h.push(make_pair(0,x0));
while(!h.empty())
{
while(!h.empty() && sel[h.top().second])
{
h.pop();
}
if(h.empty())
{
return;
}
x=h.top().second;
sel[x]=true;
for(int i=0;i<a[x].size();i++)
{
y=a[x][i].first;
c=a[x][i].second;
if(d[x]+c<d[y])
{
d[y]=d[x]+c;
}
pred[y]=x;
h.push(make_pair(-d[y],y));
}
}
}
int main()
{
citire();
int x0=1;
dijkstra(x0);
for(int i=2;i<=n;i++)
if(d[i]!=INF)
fout << d[i] << ' ';
else
fout << 0 << ' ';
return 0;
}