Pagini recente » Cod sursa (job #2604401) | Cod sursa (job #2724959) | Cod sursa (job #1957899) | Cod sursa (job #836934) | Cod sursa (job #1827176)
#include <fstream>
#include <vector>
#include <queue>
#define NM 50005
#define INF 0x3f3f3f3f
using namespace std;
vector < pair < int, int > >v[NM];
queue < int > coada;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, cost[NM];
void Dijkstra()
{
int nod2;
for(int i = 1; i <= n; ++i)
cost[i] = INF;
coada.push(1);
cost[1] = 0;
while(coada.empty() == 0)
{
nod2 = coada.front();
coada.pop();
for(int i = 0 ; i < v[nod2].size(); ++i)
{
if(cost[v[nod2][i].first] > cost[nod2] + v[nod2][i].second)
{
cost[v[nod2][i].first] = cost[nod2] + v[nod2][i].second;
coada.push(v[nod2][i].first);
}
}
}
for(int i = 2; i <= n; ++i)
{
if(cost[i] == INF) g << "0 ";
else g << cost[i] << ' ';
}
}
int main()
{
int x,y,c;
f >> n >> m;
for(int i = 1; i <= n; ++i)
{
f >> x >> y >> c;
v[x].push_back(make_pair(y,c));
}
Dijkstra();
}