Pagini recente » Cod sursa (job #267528) | Cod sursa (job #2498547) | Cod sursa (job #583949) | Cod sursa (job #756705) | Cod sursa (job #2038244)
#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#define DN 50010
#define UI unsigned int
using namespace std;
fstream fin("dijkstra.in",ios::in),fout("dijkstra.out",ios::out);
struct strv
{
public:
UI b,c;
};
struct strh
{
public:
UI d,a;
bool operator < (const strh &y) const
{
return d<y.d;
}
};
vector<strv> v[DN];
multiset<strh> heap;
UI n,m,a,b,c,dist[DN],ap[DN];
int main()
{
UI n,i,x,d,nod;
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>a>>b>>c;
v[a].push_back({b,c});
}
heap.insert({0,1});
while(heap.empty()==0)
{
d=(*heap.begin()).d;
nod=(*heap.begin()).a;
heap.erase(heap.begin());
if(ap[nod]==1) continue;
ap[nod]=1;
for(auto j:v[nod])
{
if((dist[j.b]>d+j.c) || (dist[j.b]==0 && ap[j.b]==0))
{
dist[j.b]=d+j.c;
heap.insert({dist[j.b],j.b});
}
}
}
for(i=2;i<=n;i++) fout<<dist[i]<<" ";
}