Pagini recente » Cod sursa (job #748226) | Cod sursa (job #909004) | Cod sursa (job #400322) | Cod sursa (job #2705491) | Cod sursa (job #1400669)
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int INF = 0x3f3f3f3f;
int N, M;
int D[50005];
vector<pair<int, int> > V[50005];
queue<int> Q;
void bellman()
{
memset(D, INF, sizeof(D));
D[1] = 0;
Q.push(1);
while (!Q.empty())
{
int now = Q.front();
Q.pop();
for (vector<pair<int, int> >::iterator it = V[now].begin(); it != V[now].end(); ++it)
{
if (D[now] + it->second < D[it->first])
{
D[it->first] = D[now] + it->second;
Q.push(it->first);
}
}
}
}
int main()
{
fin >> N >> M;
for (int i = 1, nod1, nod2, cost; i <= M; ++i)
{
fin >> nod1 >> nod2 >> cost;
V[nod1].push_back(make_pair(nod2, cost));
V[nod2].push_back(make_pair(nod1, cost));
}
bellman();
for (int i = 2; i <= N; ++i)
fout << D[i] << ' ';
fout << '\n';
fin.close();
fout.close();
return 0;
}