Pagini recente » Cod sursa (job #2742332) | Cod sursa (job #3343968) | Cod sursa (job #1503760) | Cod sursa (job #993350) | Cod sursa (job #3338291)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50002
#define INF (1<<30)
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N,M;
vector<int> cost(NMAX,INF),viz(NMAX,0);
vector<pair<int,int>> graph[NMAX];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
void citire()
{
fin>>N>>M;
int A,B,C;
for(int i=1; i<=M; i++)
{
fin>>A>>B>>C;
graph[A].push_back({B,C});
}
}
int main()
{
citire();
cost[1]=0;
q.push({0,1});
while(!q.empty())
{
int d=q.top().first;
int nod=q.top().second;
q.pop();
if(!viz[nod])
{
viz[nod]=1;
for(int i=0; i<graph[nod].size(); i++)
{
int next_nod=graph[nod][i].first;
int drum=graph[nod][i].second;
if(cost[next_nod]>d+drum)
{
q.push({d+drum,next_nod});
cost[next_nod]=d+drum;
}
}
}
}
for(int i=2; i<=N; i++)
{
if(cost[i]==INF)
{
cost[i]=0;
}
fout<< cost[i] << " ";
}
fout<< "\n";
return 0;
}