Pagini recente » Cod sursa (job #1029693) | Cod sursa (job #1799450) | Cod sursa (job #488552) | Cod sursa (job #2004892) | Cod sursa (job #1383312)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int NMAX = 50000;
const int INF = (1<<30);
struct DAP {
int n,c;
};
DAP make_DAP( int nod, int cost ) {
DAP aux;
aux.n = nod; aux.c = cost;
return aux;
}
struct cmp {
bool operator() ( const DAP &a, const DAP &b ) {
return ( a.c > b.c );
}
};
priority_queue< DAP, vector<DAP>, cmp > heap;
vector <DAP> v[NMAX+1];
int N,M, d[NMAX+1];
int main() {
in >> N >> M;
for( int i = 2; i <= N; ++i ) d[i] = INF;
for( int i = 1; i <= M; ++i ) {
int x,y,c;
in >> x >> y >> c;
v[x].push_back( make_DAP(y,c) );
v[y].push_back( make_DAP(x,c) );
}
heap.push( make_DAP( 1, 0 ) );
while( !heap.empty() ) {
int nod = heap.top().n;
int cost = heap.top().c;
heap.pop();
for( int i = 0; i < (int)v[nod].size(); ++i ) {
if( cost + v[nod][i].c < d[ v[nod][i].n ] ) {
d[ v[nod][i].n ] = cost + v[nod][i].c;
heap.push( make_DAP( v[nod][i].n, cost + v[nod][i].c ) );
}
}
}
for( int i = 2; i <= N; ++i ) {
if( d[i] == INF ) out << "0 ";
else out << d[i] << ' ';
}
return 0;
}