Pagini recente » Cod sursa (job #1848892) | Cod sursa (job #1574251) | Cod sursa (job #2060543) | Cod sursa (job #1354490) | Cod sursa (job #2425667)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int inf = 1e6;
int main()
{
int N, M, A, B, C;
fin>>N>>M;
vector<vector<pair<int,int> > > G(N+1);
for(int i = 1; i <= M; i++)
{
fin>>A>>B>>C;
G[A].push_back(make_pair(C,B));
}
vector<int> dist(N,inf);
dist[1] = 0;
set<pair<int,int> > Q;
Q.insert({0,1});
while(!Q.empty())
{
int current = (*Q.begin()).second;
Q.erase(Q.begin());
for(auto neighbour: G[current])
{
if(dist[current] + neighbour.first < dist[neighbour.second])
{
Q.erase(make_pair(dist[neighbour.second], neighbour.second));
dist[neighbour.second] = dist[current] + neighbour.first;
Q.insert({dist[neighbour.second], neighbour.second});
}
}
}
fin.close();
for(int i = 2; i <= N; i++)
if(dist[i] == inf) fout<<0<<' ';
else fout<<dist[i]<<' ';
fout.close();
return 0;
}