Pagini recente » Cod sursa (job #25748) | Cod sursa (job #2074123) | Cod sursa (job #849705) | Cod sursa (job #2928966) | Cod sursa (job #3186593)
#include <bits/stdc++.h>
#define N 50007
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m;
vector< pair<int,int> >edges[N];
vector<int> dist(N,-1);
vector<bool> viz(N,0);
void Citire()
{
fin >> n >> m;
for(int i=1;i<=n;i++)
{
int x,y,c;
fin >> x >> y >> c;
edges[x].push_back({c,y});
}
fin.close();
}
void Dijkstra()
{
priority_queue<pair<int,int>> pq;
pq.push({0,1});
while( !pq.empty() )
{
int x,cost;
x=pq.top().second;
cost=-pq.top().first;
pq.pop();
// cout << (dist[x]>cost or dist[x]==-1)<< " " << !viz[x];
if( (dist[x]>cost or dist[x]==-1) and !viz[x] )
{
// cout << "here";
dist[ x ]=cost;
for( auto w:edges[x] )
{
int y=w.second;
int c=w.first+cost;
if( !viz[y] )
pq.push( {-c,y} );
}
}
viz[x]=1;
}
}
int main()
{
Citire();
Dijkstra();
for(int i=2;i<=n;i++)
fout << dist[i] << " ";
fout.close();
return 0;
}