Pagini recente » Cod sursa (job #2030782) | Cod sursa (job #779029) | Cod sursa (job #2805549) | Cod sursa (job #2203898) | Cod sursa (job #3258237)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50002
#define VMAX 2000000002
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N,M,viz[NMAX];
vector <int>cost(NMAX,VMAX);
vector<vector<pair<int,int>>> graph(NMAX);
priority_queue<pair<int,int>>s;
void citire()
{
int a,b,c;
fin>>N>>M;
for(int i=1; i<=M; i++)
{
fin>>a>>b>>c;
graph[a].push_back({b,c});
}
}
int main()
{
citire();
cost[1]=0;
s.push({0,1}); ///({cost,nod});
while(!s.empty())
{
pair<int,int> x=s.top();
s.pop();
if(!viz[x.second])
{
viz[x.second]=1;
for(int i=0; i<graph[x.second].size(); i++)
{
int a,b;
a=graph[x.second][i].first;
b=graph[x.second][i].second;
if(-x.first+b<cost[a])
{
cost[a]=-x.first+b;
s.push({-cost[a],a});
}
}
}
}
for(int i=2; i<=N; i++)
{
fout<< cost[i] << " ";
}
fout<< "\n";
return 0;
}