Pagini recente » Cod sursa (job #2595788) | Cod sursa (job #2600078) | Cod sursa (job #2596023) | Cod sursa (job #2600081) | Cod sursa (job #1827172)
#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)
g << (cost[i] == INF ? 0 : 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();
}