Pagini recente » Cod sursa (job #2376676) | Monitorul de evaluare | Cod sursa (job #2078420) | Cod sursa (job #2066779) | Cod sursa (job #2425670)
#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+1,inf);
dist[1] = 0;
set<pair<int,int> > Q;
Q.insert(make_pair(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(make_pair(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;
}