Pagini recente » Profil tudorbuhnia | Cod sursa (job #667731) | Cod sursa (job #2853379) | Cod sursa (job #1639830) | Cod sursa (job #3216331)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m;
vector<pair<int,int>> L[50005];
int dist[50005];
bool viz[50005];
priority_queue<pair<int,int>> q;
void dijkstra()
{
while(!q.empty())
{
int node=q.top().second;
q.pop();
/*if(viz[node]==true)
{
continue;
}*/
for(auto vec:L[node])
{
int varf=vec.first,price=vec.second;
if(dist[node]+price<dist[varf])
{
dist[varf]=dist[node]+price;
if(!viz[varf])
{
q.push({-dist[varf],varf});
}
}
}
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,price;
fin>>x>>y>>price;
L[x].push_back({y,price});
}
q.push({0,1});
for(int i=2;i<=n;i++)
{
dist[i]=INT_MAX;
viz[i]=false;
}
dist[1]=0;
viz[1]=true;
dijkstra();
for(int i=2;i<=n;i++)
{
if(dist[i]==INT_MAX)
{
fout<<0<<" ";
}
else
{
fout<<dist[i]<<" ";
}
}
return 0;
}