Pagini recente » Cod sursa (job #2736881) | Cod sursa (job #2519877) | Cod sursa (job #2301398) | Cod sursa (job #567694) | Cod sursa (job #2542113)
#include<bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int dist[50010];
int n,m;
struct node
{
int index,distance;
};
struct comparator
{
bool operator()(const node&a,const node&b)
{
return a.distance<b.distance;
}
};
vector<std::pair<int,int>> drumuri [50010];
void read()
{
int a,b,c;
in>>n>>m;
for(int i=0; i<n; i++)
dist[i]=-1;
for(int i=0; i<m; i++)
{
in>>a>>b>>c;
drumuri[a-1].push_back(std::pair<int,int>(b-1,c));
}
}
void dijkstra()
{
priority_queue<node,vector<node>,comparator> nodes;
nodes.push({0,0});
while(nodes.size())
{
node top=nodes.top();
nodes.pop();
int index=top.index;
int val=top.distance;
if(dist[index]==-1 || dist[index]>val)
{
dist[index]=val;
for(unsigned i=0; i<drumuri[index].size(); i++)
{
int nv=val+drumuri[index][i].second;
int nindex=drumuri[index][i].first;
nodes.push({nindex,nv});
}
}
}
}
void prt()
{
for(int i=1; i<n; i++)
out<<max(dist[i],0)<<" ";
}
int main()
{
read();
dijkstra();
prt();
return 0;
}