Pagini recente » Cod sursa (job #2555624) | Cod sursa (job #2933118) | Cod sursa (job #1977637) | Poze preONI 2007 - concurs | Cod sursa (job #1480592)
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
ifstream fin ("djikstra.in");
ofstream fout ("djikstra.out");
int n,m,cost[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
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 (!cost[i] || cost[i] >= cost[k] + costt)
{
cost[i] = cost[k] + costt;
q.push(i);
}
}
}
}
void Print_that()
{
for (int i=2; i<=n; i++)
fout << cost[i] << " ";
fout << "\n";
}
int main ()
{
Citire();
Lee_Bellman_Ford_BFS_Dijkstra();
Print_that();
fin.close();
fout.close();
return 0;
}