Pagini recente » Cod sursa (job #2418592) | Cod sursa (job #1536209) | Cod sursa (job #330387) | Cod sursa (job #248986) | Cod sursa (job #2831642)
#include <bits/stdc++.h>
using namespace std;
const int N = 50010;
const int oo = 2000000010;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m, viz[N],d[N];
vector<pair<int,int>> v[N];
priority_queue <pair<int,int>> q;
void Dijkstra(int source)
{
int minn, i, k, pas, cost;
for(int i=2; i<=n; i++)
d[i] = oo;
q.push({0,1});
while(!q.empty())
{
k= q.top().second;
q.pop();
if(viz[k] == 0)
{
viz[k] = 1;
for(auto it : v[k])
{
cost = it.first;
i = it.second;
if(d[i] > d[k] + cost)
{
d[i] = d[k] + cost;
q.push({-d[i],i});
}
}
}
}
}
int main()
{
int x,y,cost;
fin>>n>>m;
while(m--)
{
fin>>x>>y>>cost;
v[x].push_back({cost,y});
}
Dijkstra(1);
for(int i=2;i<=n;i++)
if(d[i]== oo)
fout<<"0 ";
else
fout<<d[i]<<" ";
return 0;
}