Pagini recente » Cod sursa (job #2922572) | Cod sursa (job #3241217) | Cod sursa (job #2927028) | Cod sursa (job #321259) | Cod sursa (job #3039951)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
int n,m;
struct element
{
int d,vf;
};
class Comparare
{
public:
bool operator() (element a,element b)
{
if (a.d < b.d)
return true;
return false;
}
};
priority_queue <element,vector<element>,Comparare> q;
vector< pair<int,int> > g[50005];
int d[50005];
void dijkstra ()
{
int i;
for(i=1;i<=n;i++)
d[i] = 2e9;
d[1] = 0;
element aux;
aux.d = 0;
aux.vf = 1;
q.push(aux);
while (!q.empty())
{
aux = q.top();
q.pop();
if (d[aux.vf]!=aux.d)
continue;
int vf = aux.vf;
for (auto muchie:g[vf])
{
int nod = muchie.first;
int c = muchie.second;
if (d[nod] > d[vf] + c)
{
d[nod] = d[vf] + c;
aux.vf = nod;
aux.d = d[nod];
q.push(aux);
}
}
}
for(i=2;i<=n;i++)
{
if (d[i]==2e9)
{
cout << "0 ";
continue;
}
cout << d[i] << ' ';
}
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int i;
for (i=1;i<=m;i++)
{
int x,y,c;
cin >> x >> y >> c;
g[x].push_back({y,c});
}
dijkstra();
return 0;
}