Pagini recente » Cod sursa (job #2072839) | Cod sursa (job #1497368) | Cod sursa (job #1093846) | Cod sursa (job #2358046) | Cod sursa (job #2425514)
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
using namespace std;
const int N = 50100;
const int INF = 0x3f3f3f3f;
vector< pair<int, int> > G [N];
set< pair<int, int> >Set;
int d[N], n;
bool viz[N];
void init (){
for ( int i = 1; i <= n; i++ )
d[i] = INF;
}
void citire_date (){
int m, i, x, y, cost;
//ifstream in ("date.in");
ifstream in("dijkstra.in");
in >> n >> m;
for ( i = 0; i < m; i++ ){
in >> x >> y >> cost;
G[x].push_back ( make_pair(y, cost));
}
in.close();
init();
}
void dijkstra ( ){
int aux, i;
d[1] = 0;
Set.insert ( {1, 0} );
while ( not Set.empty() ){
aux = (*Set.begin()).first;
Set.erase ( Set.begin());
if ( viz[aux] )
continue;
viz[aux] = true;
for ( i = 0; i < G[aux].size(); i++)
if ( not viz[G[aux][i].first] && d[aux] + G[aux][i].second < d[G[aux][i].first]){
d[G[aux][i].first] = d[aux] + G[aux][i].second;
Set.insert ({G[aux][i].first, d[G[aux][i].first]});
}
}
}
void afisare_date (){
//ofstream out ("date.out");
ofstream out ("dijkstra.out");
for ( int i = 2; i <= n; i++ )
out << ( d[i] == INF ? 0 : d[i] ) << " ";
out.close();
}
int main (){
citire_date();
dijkstra();
afisare_date();
return 0;
}