Pagini recente » Cod sursa (job #3251915) | Cod sursa (job #27279) | Cod sursa (job #869615) | Cod sursa (job #19758) | Cod sursa (job #3258257)
#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,viz[NMAX];
vector <int>cost(NMAX,INF);
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]==1)
{
continue;
}
viz[x.second]=1;
for(int i=0; i<(int)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++)
{
if(cost[i]==INF)
{
cost[i]=0;
}
fout<< cost[i] << " ";
}
fout<< "\n";
return 0;
}