Pagini recente » Cod sursa (job #299755) | Monitorul de evaluare | Cod sursa (job #3037037) | Cod sursa (job #73624) | Cod sursa (job #3342601)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50000;
int n, m;
int x, y, cost;
vector < pair<int,int> > v[NMAX+1];
int dist_max[NMAX+1];
void dist()
{
priority_queue < pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > pq;
dist_max[1] = 0;
for(int i=2; i<=n; i++)
dist_max[i] = INT_MAX;
pq.push(make_pair(0,1));
while(!pq.empty())
{
pair<int,int> nod = pq.top();
pq.pop();
if(dist_max[nod.second] < nod.first){continue;}
for(auto a : v[nod.second])
{
if(dist_max[a.first] > dist_max[nod.second] + a.second)
{
dist_max[a.first] = dist_max[nod.second] + a.second;
pq.push(make_pair(dist_max[a.first], a.first));
}
}
}
}
int main()
{
fin >> n >> m;
for(int i=1; i<=m; i++)
{
fin >> x >> y >> cost;
v[x].push_back(make_pair(y,cost));
}
dist();
for(int i = 2; i <= n; i++)
{
if(dist_max[i] == INT_MAX)
fout << 0 << ' ';
else
fout << dist_max[i] << ' ';
}
return 0;
}