Pagini recente » Cod sursa (job #424947) | Cod sursa (job #1566618) | Cod sursa (job #2484570) | Cod sursa (job #2449738) | Cod sursa (job #3186591)
#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);
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();
if( dist[x]!=-1 )continue;
dist[ x ]=cost;
for( auto w:edges[x] )
{
int y=w.second;
int c=w.first+cost;
if( dist[y]==-1 )pq.push( {-c,y} );
}
}
}
int main()
{
Citire();
Dijkstra();
for(int i=2;i<=n;i++)
fout << dist[i] << " ";
fout.close();
return 0;
}