Pagini recente » Cod sursa (job #2000857) | Cod sursa (job #2184643) | Cod sursa (job #2773886) | Cod sursa (job #445525) | Cod sursa (job #2365984)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
const int Nmax = 50005;
struct G
{
int node , val;
bool operator < (const G &e) const
{
return val > e.val;
}
};
priority_queue < G > Q;
int n , m , dist[Nmax];
vector < pair < int , int > > L[Nmax];
bool viz[Nmax];
int main()
{
int x , y , c;
G w;
fin >> n >> m;
for(int i = 1 ; i <= n ; i++)
{
fin >> x >> y >> c;
L[x].push_back({y , c});
L[y].push_back({x , c});
}
for(int i = 1 ; i <= n ; i++)
dist[i] = 1e9;
dist[1] = 0;
Q.push({1 , 0});
while(!Q.empty())
{
w = Q.top();
Q.pop();
if(!viz[w.node])
{
viz[w.node] = true;
for(auto it : L[w.node])
if(dist[it.first] > dist[w.node] + it.second)
{
dist[it.first] = dist[w.node] + it.second;
Q.push({it.first , dist[it.first]});
}
}
}
for(int i = 2 ; i <= n ; i++)
if(dist[i] == 1e9)
fout << "0 ";
else fout << dist[i] << " ";
fout << "\n";
fin.close();
fout.close();
return 0;
}