Pagini recente » Cod sursa (job #2656602) | Cod sursa (job #599000) | Cod sursa (job #2706301) | Cod sursa (job #2827323) | Cod sursa (job #3171416)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int MAX_NODES = 50000;
vector<pair<int, int>> graph[MAX_NODES + 1];
int minDistance[MAX_NODES + 1];
void gen(int noNodes, int startNode) {
for (vector<pair<int, int>>::iterator it = graph[startNode].begin(); it < graph[startNode].end(); ++it) {
if (minDistance[it->first] == 0) {
minDistance[it->first] = minDistance[startNode] + it->second;
} else {
minDistance[it->first] = min(minDistance[startNode] + it->second, minDistance[it->first]);
}
gen(noNodes, it->first);
}
}
int main() {
int noNodes, noArches;
fin >> noNodes >> noArches;
for (int i = 1; i <= noArches; ++i) {
int start, end, distance;
fin >> start >> end >> distance;
pair<int, int> info;
info.first = end;
info.second = distance;
graph[start].push_back(info);
}
gen(noNodes, 1);
for (int i = 2; i <= noNodes; ++i) {
fout << minDistance[i] << ' ';
}
return 0;
}
/*
5 6
1 2 1
1 4 2
4 3 4
2 3 2
4 5 3
3 5 6
=>
1 3 2 5
5 6
1 4 10
1 3 9
1 2 8
4 5 9
3 5 10
2 5 11
=>
8 9 10 19
7 7
1 4 10
1 3 9
1 2 8
4 5 9
3 5 10
2 5 11
6 7 100
=>
8 9 10 19 0 0
3 1
2 3 20000
=>
0 0
*/