Pagini recente » Cod sursa (job #244827) | Cod sursa (job #945524) | Cod sursa (job #1697429) | Cod sursa (job #2536727) | Cod sursa (job #3180431)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <deque>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int MAX_NODES = 50000;
const long long MAX_DISTANCE = 5000000000;
vector<pair<int, int>> graph[MAX_NODES + 1];
priority_queue<int> currentNodes;
pair<int, int> arch;
long long minDistance[MAX_NODES + 1];
int main() {
int noNodes, noArches;
fin >> noNodes >> noArches;
for (int i = 1; i <= noArches; ++i) {
int startNode;
// pair<int, int> arch;
fin >> startNode >> arch.first >> arch.second;
graph[startNode].push_back(arch);
}
for (int i = 2; i <= noNodes; ++i) {
minDistance[i] = MAX_DISTANCE + 1;
}
minDistance[1] = 0;
currentNodes.push(1);
while (currentNodes.empty() == 0) {
int startNode = currentNodes.top();
currentNodes.pop();
for (vector<pair<int, int>>::iterator it = graph[startNode].begin(); it < graph[startNode].end(); ++it) {
if (minDistance[startNode] + it->second < minDistance[it->first]) {
minDistance[it->first] = minDistance[startNode] + it->second;
currentNodes.push(it->first);
}
}
}
// currentNodes.push_back(1);
/* while (currentNodes.size() > 0) {
for (deque<int>::iterator it = currentNodes.begin(); it < currentNodes.end(); ++it) {
for (vector<pair<int, int>>::iterator it2 = graph[*it].begin(); it2 < graph[*it].end(); ++it2) {
if (minDistance[*it] + it2->second < minDistance[it2->first]) {
minDistance[it2->first] = minDistance[*it] + it2->second;
nextNodes.push_back(it2->first);
}
}
}
currentNodes = nextNodes;
nextNodes.clear();
}*/
for (int i = 2; i <= noNodes; ++i) {
if (minDistance[i] == MAX_DISTANCE + 1) {
minDistance[i] = 0;
}
fout << minDistance[i] << ' ';
}
return 0;
}