Pagini recente » Cod sursa (job #1060344) | Cod sursa (job #279348) | Cod sursa (job #2335938) | Cod sursa (job #1325663) | Cod sursa (job #2755570)
#include <fstream>
#include <vector>
#include <deque>
#include <cstring>
#include <queue>
#include <limits.h>
#include <unordered_set>
using namespace std;
ifstream cin("dijkstra.in") ;
ofstream cout("dijkstra.out") ;
int n ;
vector<pair<int, int> > v[50009] ;
int cost[50009], tatal[50009], verif[50009] ;
int main()
{
int q ;
cin >> n >> q ;
while(q --)
{
int a, b, c ;
cin >> a >> b >> c ;
v[a].push_back({b, c}) ;
}
for(int f = 1 ; f <= n ; f ++)
cost[f] = INT_MAX ;
/*
int vf = 1 ;
cost[1] = 0 ;
viz[1] = 1 ;
while(vf)
{
int mnf = 0 ;
for(int f = 1 ; f < v[vf].size() ; f ++)
if(v[vf][f].second < v[vf][mnf].second)mnf = f ;
mnf = v[vf][mnf].first ;
/// mnf este nodul in care mergem de aici
cost[mnf]
}
*/
priority_queue<pair<int, int> > pq ;
pq.push({0, 1}) ; /// intai cost si dupa nod
cost[1] = 0 ;
while(pq.size())
{
int nod_val = pq.top().first ;
int nod_curent = pq.top().second ;
pq.pop() ;
for(int f = 0 ; f < v[nod_curent].size() ; f ++) /// extindem pathul acesta
{
int nod_destinatie = v[nod_curent][f].first ;
int cost_local = v[nod_curent][f].second ;
/// trebuie vazut daca nod_val + cost_local este mai mic ca cost
if(nod_val + cost_local < cost[nod_destinatie])
{
cost[nod_destinatie] = nod_val + cost_local ;
pq.push({cost[nod_destinatie], nod_destinatie}) ;
}
}
}
for(int f = 1 ; f <= n ; f ++)
if(cost[f] == INT_MAX)cost[f] = 0 ;
for(int f = 2 ; f <= n ; f ++)
cout << cost[f] << " " ;
return 0;
}