Pagini recente » Cod sursa (job #1792019) | Cod sursa (job #2102339) | Cod sursa (job #2433572) | Cod sursa (job #1637606) | Cod sursa (job #2255766)
#include <bits/stdc++.h>
using namespace std;
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[50005];
int dp[50005];
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, 1 << 30);
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] != 1 << 30)
out << dp[i];
else
out << 0;
out << " ";
}
return 0;
}