Pagini recente » Cod sursa (job #460996) | Cod sursa (job #3203215) | Cod sursa (job #255012) | Cod sursa (job #2891701) | Cod sursa (job #3300600)
#include <fstream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax=50005;
vector<pair<int,int>> l[nmax];
priority_queue<pair<int,int>>q;
vector<int> mindist(nmax,INT_MAX);
void dijkstra()
{
q.push({0,1});
while(!q.empty())
{
auto aux=q.top();
q.pop();
int nod_dist=-aux.first;
int nod=aux.second;
if(nod_dist > mindist[nod])
{
continue;
}
for(auto vec : l[nod])
{
if(mindist[vec.first] > mindist[nod] + vec.second)
{
mindist[vec.first] = mindist[nod] + vec.second;
}
q.push({-mindist[vec.first],vec.first});
}
}
}
int main()
{
mindist[1]=0;
int n,m;
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,cost;
fin>>x>>y>>cost;
l[x].push_back({y,cost});
}
dijkstra();
for(int i=2;i<=n;i++)
{
fout<<mindist[i]<<'\n';
}
return 0;
}