Pagini recente » Cod sursa (job #1072461) | Cod sursa (job #486674) | Cod sursa (job #1005420) | Cod sursa (job #1533941) | Cod sursa (job #1918512)
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector < pair < int, int > > G[ 50005 ];
int t, n, m, cost[ 50005 ], viz[ 50005 ];
queue < int > q;
int main()
{
int x, y, c, nod, nod2, costcurent, sizec;
f >> n >> m;
for(int i = 1; i <= n; ++i) cost[ i ] = inf;
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();
viz[ nod ] = 0;
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 ] = cost[ nod ] + costcurent;
if(!viz[ nod2 ])
{
q.push( nod2 );
viz[ nod2 ] = 1;
}
}
}
}
for(int i = 2; i <= n; ++i)
{
if(cost[ i ] != inf) g << cost[ i ] << ' ';
else g << "0" << ' ';
}
return 0;
}