Pagini recente » Borderou de evaluare (job #1183721) | Cod sursa (job #3220687) | Borderou de evaluare (job #2019464) | Cod sursa (job #383179) | Cod sursa (job #2342701)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n, m, from, to, cost,dp[50005];
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
class cmp{
public:
bool operator () (pair<int,int>a,pair<int,int>b)
{
return a.second>b.second;
}
};
vector<pair<int,int>>graph[50005];
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp>q;
void solve()
{
q.push({1,0});
while (!q.empty())
{
pair<int,int>node=q.top();
q.pop();
for (auto &v:graph[node.first])
{
if (dp[node.first]+v.second<dp[v.first])
{
dp[v.first]=dp[node.first]+v.second;
q.push({v.first,dp[v.first]});
}
}
}
}
int main() {
f >> n >> m;
for (int i=2; i<=n; i++)
{
dp[i]=0x3f3f3f3f;
}
for (int i=1; i<=m; ++i)
{
f >> from >> to >> cost;
graph[from].push_back({to,cost});
}
solve();
for (int i=2; i<=n; i++)
{
g << dp[i] <<' ';
}
return 0;
}