Pagini recente » Cod sursa (job #454731) | Cod sursa (job #2057070) | Cod sursa (job #440971) | Cod sursa (job #2107964) | Cod sursa (job #2854354)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
const int infinite=20002;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct Node
{
int node,dist;
bool operator < (const Node &other)const
{
return dist>other.dist;
}
};
int n,m;
int distMin[50001];
bool viz[50001];
vector<Node> child[50001];
priority_queue<Node> pq;
void Dijstrka()
{
pq.push({1,0});
while(!pq.empty())
{
int u=pq.top().node;
int cost=pq.top().dist;
pq.pop();
if(viz[u]==false)
{
viz[u]=true;
distMin[u]=cost;
for(auto i:child[u])
{
if(viz[i.node]==false)
pq.push({i.node,cost+i.dist});
}
}
}
for(int i=2;i<=n;i++)
g<<distMin[i]<<" ";
}
void Citire()
{
f>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,cst;
f>>x>>y>>cst;
child[x].push_back({y,cst});
}
}
int main()
{
Citire();
Dijstrka();
}