Pagini recente » Cod sursa (job #2109021) | Cod sursa (job #2712639) | Cod sursa (job #953455) | Cod sursa (job #325526) | Cod sursa (job #2960783)
#include <fstream>
#define max_value 1<<30
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int d[50010],t[50010],n,m,i,x,y,c;
bool viz[50010];
struct NodeCost{
int node, cost;
bool operator < (const NodeCost other) const {
return cost > other.cost;
}
};
vector<NodeCost>v[50010];
priority_queue<NodeCost>q;
void dijkstra(int nod)
{
int i,curentDist,curentNod,nr;
for(i=1; i<=n; i++)
d[i]=max_value;
q.push({nod,0});
while(!q.empty())
{
curentNod=q.top().node;
curentDist=q.top().cost;
q.pop();
if(d[curentNod]!=max_value)
continue;
d[curentNod]=curentDist;
for(auto it: v[curentNod])
{
if(d[it.node]==max_value)
{
q.push({it.node,curentDist+it.cost});
}
}
}
}
int main()
{
cin>>n>>m;
for(i=1; i<=m; i++)
{
cin>>x>>y>>c;
v[x].push_back({y,c});
}
dijkstra(1);
for(i=2; i<=n; i++)
{
if(d[i]==max_value)
cout<<0<<" ";
else
cout<<d[i]<<" ";
}
return 0;
}