Pagini recente » Cod sursa (job #763951) | Cod sursa (job #2544984) | Cod sursa (job #2335244) | Cod sursa (job #1722164) | Cod sursa (job #2869206)
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <deque>
#include <vector>
#include <set>
#include <queue>
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
const int MAX_SIZE = 10005;
const int MAX_VAL = 2000000000;
vector<pair<int, int>> graph[MAX_SIZE];
int costs[MAX_SIZE];
void dijkstra(int peaks) {
for (int i = 2; i <= peaks; ++i) {
costs[i] = MAX_VAL;
}
set<pair<int, int>> positions;
positions.insert({0, 1});
while (!positions.empty()) {
int currentCost = positions.begin()->first;
int currentPeak = positions.begin()->second;
positions.erase(positions.begin());
//cout << currentCost << " " << currentPeak << "\n";
if (currentCost == costs[currentPeak]) {
for (pair<int, int> element : graph[currentPeak]) {
int nextPeak = element.first;
int nextCost = element.second;
if (costs[nextPeak] > currentCost + nextCost) {
costs[nextPeak] = currentCost + nextCost;
positions.insert({costs[nextPeak], nextPeak});
}
}
}
}
fout << costs[peaks];
}
int main() {
int peaks, arches;
int k;
fin >> peaks >> arches >> k;
for (int i = 1; i <= k; ++i) {
int city;
fin >> city;
}
for (int i = 1; i <= arches; ++i) {
int start, end, cost;
fin >> start >> end >> cost;
graph[start].push_back(make_pair(end, cost));
}
dijkstra(peaks);
}
/*
2 1 0
1 2 10
10 10 0
1 10 100
1 2 100
2 3 100
3 4 100
4 5 100
5 6 100
6 7 100
7 8 100
8 9 100
9 10 100
4 5 0
1 2 1
1 3 1
2 3 1
2 4 4
3 4 2
*/