Pagini recente » Cod sursa (job #1695802) | Cod sursa (job #2443884) | Cod sursa (job #221662) | Cod sursa (job #2849933) | Cod sursa (job #1893925)
#include <bits/stdc++.h>
using namespace std;
#define maxN 50005
#define INF 1<<30
struct drum{
int destinatie,cost;
drum *next;
};
drum *listaNoduri[maxN];
int stiva[1000000];
int main()
{
ios_base::sync_with_stdio(false);
ifstream input("dijkstra.in");
ofstream output("dijkstra.out");
int n,m;
input >> n >> m;
int cost[maxN];
for(int i=1; i<=n; ++i) cost[i] = INF;
for(int i=1;i <= m; ++i){
int x,y,z;
input >> x >> y >> z;
drum *temp = new drum;
*temp = {y,z};
temp->next = listaNoduri[x];
listaNoduri[x] = temp;
}
int index = 0, topLevel = 0;
cost[1] = 0;
for( stiva[0] = 1 ; index <= topLevel ; ++index ){
int nodCurent = stiva[index];
drum *temp = listaNoduri[nodCurent];
for( ; temp ; temp = temp->next ){
int nodNou = temp->destinatie;
int costDeplasare = temp ->cost;
if( cost[nodNou] > (cost[nodCurent] + costDeplasare) ){
cost[nodNou] = (cost[nodCurent] + costDeplasare);
stiva[++topLevel] = nodNou;
}
}
}
int error_value = cost[0];
for(int i=2;i<=n;++i){
if( cost[i] == INF )
output << "0 ";
else
output << cost[i] << " ";
}
return 0;
}