Pagini recente » Cod sursa (job #841129) | Cod sursa (job #1273792) | Cod sursa (job #2108331) | Cod sursa (job #1551260) | Cod sursa (job #1428053)
#include <fstream>
#include <vector>
#include <set>
#define nmax 250000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int N, M;
bool seen[nmax];
set <pair <int, int> > S;
int costuri[nmax];
vector <pair <int,int>> graf[nmax];
int main()
{int a, cost, b, i;
f>>N>>M;
for(i = 1; i <= M; ++i){
f>>a>>b>>cost;
graf[a].push_back(make_pair(b, cost));
}
for(i = 2; i <= N; ++i)
costuri[i] = nmax;
costuri[1] = 0;
S.insert( make_pair(0, 1) );
while( !S.empty() ){
int val = ( *S.begin() ).first, nod = ( *S.begin() ).second;
S.erase( *S.begin() );
for(i = 0; i < graf[nod].size(); ++i){
if(costuri[graf[nod][i].first] > val + graf[nod][i].second){
costuri[graf[nod][i].first] = val + graf[nod][i].second;
S.insert( make_pair(costuri[graf[nod][i].first], graf[nod][i].first) );
}
}
}
for(i = 2; i <= N; ++i){
if(costuri[i] != nmax)
g<<costuri[i]<<' ';
else g<<0<<' ';
}
g<<'\n';
return 0;
}