Pagini recente » Cod sursa (job #642529) | Cod sursa (job #49662) | Cod sursa (job #3041078) | Cod sursa (job #721463) | Cod sursa (job #1842905)
#include <fstream>
#include <vector>
#include <queue>
#define NM 50005
using namespace std;
vector < pair < int, int > > G[NM];
queue < int > Q;
int n, m, cost[NM], x,y, c;
const int INF = 2000000000;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int main()
{
f >> n >> m;
for(int i = 1; i <= m; ++i)
{
f >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
for(int i = 2; i <= n; ++i)
cost[i] = INF;
Q.push(1);
cost[1] = 0;
while( !Q.empty() )
{
x = Q.front();
Q.pop();
for(int i = 0; i < G[x].size(); ++i)
{
y = G[x][i].first;
if(cost[y] > cost[x] + G[x][i].second)
{
cost[y] = cost[x] + G[x][i].second;
Q.push(y);
}
}
}
for(int i = 2; i <= n; ++i)
{
if(cost[i] == INF) cost[i] = 0;
g << cost[i] << ' ';
}
return 0;
}