Pagini recente » Cod sursa (job #793053) | Rating Paula Dobanda (pauladobanda) | Rating Diles Miles (dilesmavis) | Atasamentele paginii Clasament olimpiada_info | Cod sursa (job #1792115)
#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30);
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int maxn = 50005;
class cmp {
public:
bool operator()( pair<int, int> a, pair<int, int> b ) {
return a.second > b.second;
}
}; priority_queue<pair<int, int>, vector< pair<int, int> >, cmp> pq;
vector <pair <int, int> > g[maxn];
int dp[maxn];
int main( ) {
int n, m;
in >> n >> m;
for( int i = 1; i <= m; i ++ ) {
int x, y, z;
in >> x >> y >> z;
g[x].push_back( make_pair(y, z) );
}
fill( dp + 1, dp + n + 1, INF );
dp[1] = 0; pq.push( make_pair(1, 0) );
while( !pq.empty() ) {
pair<int, int> p = pq.top(); pq.pop();
if( p.second > dp[p.first] )
continue;
for(auto it : g[p.first])
{
int nod = it.first, cost = it.second;
if(dp[nod] > dp[p.first] + cost)
{
dp[nod] = dp[p.first] + cost;
pq.push(make_pair(nod, dp[nod]));
}
}
}
for(int i = 2; i <= n; i++)
{
if(dp[i] != INF)
out << dp[i];
else
out << 0;
out << " ";
}
return 0;
}