Pagini recente » Cod sursa (job #54843) | Cod sursa (job #1921414) | Cod sursa (job #165002) | Cod sursa (job #503560) | Cod sursa (job #2576769)
#include <bits/stdc++.h>
using namespace std;
const int dim = 50010;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m, dist[dim];
vector<pair<int, int> > muchii[dim];
struct cmp
{
bool operator() (const int& a, const int& b) const
{
return dist[a] < dist[b];
}
};
int main()
{
in>>n>>m;
int x, y, c;
for(int i = 1; i <= m; i++)
{
in>>x>>y>>c;
muchii[x].push_back({y, c});
}
vector<pair<int, int> >::iterator it;
set<int> s;
int nod;
dist[1] = 0;
s.insert(1);
while(!s.empty())
{
nod = *(s.begin());
s.erase(s.begin());
for(it = muchii[nod].begin(); it != muchii[nod].end();it++)
{
if(dist[it->first] > dist[nod] + it->second || dist[it->first] == 0)
{
dist[it->first] = dist[nod] + it->second;
s.insert(it->first);
}
}
}
for(int i = 2; i <= n; i++)
out<<dist[i]<<' ';
return 0;
}