Pagini recente » Cod sursa (job #873289) | Cod sursa (job #1057308) | Cod sursa (job #2052973) | Cod sursa (job #2182619) | Cod sursa (job #1918533)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#define inf 2000000000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector < pair < int, int > > G[ 50005 ];
int t, n, m, cost[ 50005 ];
queue < int > q;
int main()
{
int x, y, c, nod, nod2, costcurent, sizec;
f >> n >> m;
for(int i = 1; i <= m ;++i)
{
f >> x >> y >> c;
G[ x ].push_back(make_pair(y, c));
}
q.push( 1 );
cost[ 1 ] = 0;
while(!q.empty())
{
nod = q.front();
q.pop();
sizec = G[ nod ].size();
for(int i = 0; i < sizec; ++i)
{
costcurent = G[ nod ][ i ].second;
nod2 = G[ nod ][ i ].first;
if(cost[ nod2 ] > cost[ nod ] + costcurent || (cost[nod2] == 0 && nod2 != 1))
{
cost[ nod2 ] = cost[ nod ] + costcurent;
q.push( nod2 );
}
}
}
for(int i = 2; i <= n; ++i)
{
if(cost[ i ] == inf) g << 0 << ' ';
else g << cost[ i ] << ' ';
}
return 0;
}