Pagini recente » Cod sursa (job #1232652) | Cod sursa (job #198541) | Cod sursa (job #866514) | Cod sursa (job #837671) | Cod sursa (job #2444289)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define pb push_back
#define x first
#define y second
#define pii pair<int,int>
priority_queue<pii, vector<pii>, greater<pii> > Q;
const int Nmax = 50000 + 5;
vector<pii>v[Nmax];
int n, m, dp[Nmax];
bool inq[Nmax];
int main()
{
fin >> n >> m;
for(int i = 1, a, b, c; i <= m; ++i)
{
fin >> a >> b >> c;
v[a].pb({b, c});
}
for(int i = 1; i <= n; ++i)dp[i] = (1 << 30);
dp[1] = 0; inq[1] = 1; Q.push({0, 1});
while(!Q.empty())
{
int nod = Q.top().y;
inq[nod] = 0;
Q.pop();
for(auto i : v[nod])
{
if(dp[i.x] > dp[nod] + i.y)
{
dp[i.x] = dp[nod] + i.y;
if(!inq[i.x])
{
Q.push({dp[i.x], i.x});
inq[i.x] = 1;
}
}
}
}
for(int i = 2; i <= n; ++i)
if(dp[i] != 1<< 30)fout << dp[i] << ' ';
else fout << 0 << ' ';
return 0;
}