Pagini recente » Cod sursa (job #620815) | Istoria paginii utilizator/lilianagalbenu | Cod sursa (job #470175) | Istoria paginii runda/la_intamplare1/clasament | Cod sursa (job #1480586)
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
ifstream fin ("djikstra.in");
ofstream fout ("djikstra.out");
int n,m,cost[50005];
bool viz[50005];
vector <int> L[50005];
queue <int> q;
void Citire()
{
int x,y,costt;
fin >> n >> m;
for (int i=1; i<=m; i++)
{
fin >> x >> y >> costt;
L[x].push_back(y);
L[x].push_back(costt);
}
}
void Lee_Bellman_Ford_BFS_Dijkstra()
{
int i,j,k,costt;
q.push(1); // in coada stau noduri
viz[1]=1;
cost[1]=0;
while (!q.empty())
{
k = q.front();
q.pop();
for (int j=0; j<L[k].size(); j=j+2)
{
i = L[k][j]; costt = L[k][j+1];
if (!viz[i] || cost[i] >= cost[k] + costt)
{
cost[i] = cost[k] + costt;
viz[i] = 1;
q.push(i);
}
}
}
}
void Print_that()
{
for (int i=2; i<=n; i++)
fout << cost[i] << " ";
fout << endl;
}
int main ()
{
Citire();
Lee_Bellman_Ford_BFS_Dijkstra();
Print_that();
fin.close();
fout.close();
return 0;
}