Pagini recente » Cod sursa (job #2073369) | Cod sursa (job #2130217) | Cod sursa (job #297798) | Cod sursa (job #2500019) | Cod sursa (job #3196468)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m,d[50001];
struct numar
{
int x,c;
};
vector <numar> A[50001];
struct cmp
{
bool operator () (const int& a, const int& b)
{
return d[a]>d[b];
}
};
priority_queue< int, vector <int> , cmp> q;
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,c;
fin>>x>>y>>c;
A[x].push_back({y,c});
}
for(int i=1;i<=n;i++)
{
d[i]=100000001;
}
d[1]=0;
q.push(1);
while(!q.empty())
{
int x=q.top();
q.pop();
for(auto i :A[x])
{
int y=i.x;
int c=i.c;
if(d[y]>d[x]+c)
{
d[y]=d[x]+c;
q.push(y);
}
}
}
for(int i=2;i<=n;i++)
fout<<d[i]<<" ";
return 0;
}