Pagini recente » Cod sursa (job #1838518) | Cod sursa (job #2150828) | Cod sursa (job #1260862) | Cod sursa (job #1625036) | Cod sursa (job #2669133)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
auto cmp = [] (pair <int,int> a, pair <int,int> b)
{
return a.second > b.second;
};
int n,m,x,y,a,b,i,pret,cost[50001];
bitset <50001> trecut;
vector <vector<pair<int,int>>> drumuri;
priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> pq(cmp);
int main()
{
ios::sync_with_stdio(0);
f >> n >> m;
drumuri.resize(n + 1);
for(i = 0; i < n; ++ i)
{
f >> x >> y >> pret;
drumuri[x].push_back({y,pret});
}
for(i = 2; i <= n; ++ i)
cost[i] = inf;
for(i = 0; i < drumuri[1].size(); ++ i)
{
a = drumuri[1][i].first;
b = drumuri[1][i].second;
cost[a] = b;
pq.push({a,b});
}
trecut[1] = 1;
while(!pq.empty())
{
a = pq.top().first;
b = pq.top().second;
pq.pop();
if(trecut[a])
continue;
trecut[a] = 1;
for(i = 0; i < drumuri[a].size(); ++ i)
{
x = drumuri[a][i].first;
y = drumuri[a][i].second;
if(!trecut[x] && cost[x] > y + b)
{
cost[x] = y + b;
pq.push({x,y + b});
}
}
}
for(i = 2; i <= n; ++ i)
if(!trecut[i])
g << 0 << ' ';
else
g << cost[i] << ' ';
}