Pagini recente » Cod sursa (job #396153) | Cod sursa (job #3173230) | Cod sursa (job #674135) | Cod sursa (job #1140576) | Cod sursa (job #3222744)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int NMAX = 5e4 + 5;
const int INF = 2e9;
int n;
int dist[NMAX];
int rasp_dat[NMAX];
bool viz[NMAX];
struct Radu{
int next, cost;
};
vector <Radu> g[NMAX];
struct HeapNode{
int next, cost;
bool operator < (const HeapNode &other) const {
return other.cost < cost;
}
};
void dijkstra( int node ){
priority_queue <HeapNode> pq;
dist[node] = 0;
pq.push({node, 0});
while( !pq.empty() ){
node = pq.top().next;
pq.pop();
if( viz[node] )
continue ;
viz[node] = true;
for( auto vec : g[node] ){
if( dist[node] + vec.cost < dist[vec.next] ){
dist[vec.next] = dist[node] + vec.cost;
pq.push({vec.next, dist[vec.next]});
}
}
}
// int i = 1;
// while( i <= n && dist[i] == rasp_dat[i] )
// i++;
// if( i == n+1 )
// return 1;
// return 0;
for( int i = 2; i <= n; i++ )
if( dist[i] == INF )
out << "0 ";
else
out << dist[i] << " ";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
while( t-- ){
int m;
in >> n >> m;
for( int i = 1; i <= n; i++ ){
// in >> rasp_dat[i];
dist[i] = INF;
viz[i] = false;
}
for( int i = 0; i < m; i++ ){
int a, b, c;
in >> a >> b >> c;
g[a].push_back({b, c});
// g[b].push_back({a, c});
}
dijkstra(1);
// if( dijkstra(s) ) out << "DA\n";
// else out << "NU\n";
for( int i = 1; i <= n; i++ )
g[i].clear();
}
return 0;
}