Pagini recente » Cod sursa (job #3257898) | Cod sursa (job #9262) | Cod sursa (job #2916660) | Cod sursa (job #2918503) | Cod sursa (job #2973224)
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <deque>
#include <vector>
#include <set>
#include <queue>
#include <bitset>
#include <limits.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAX_SIZE = 30000;
int noVillages, noRoutes, start, finish;
vector<pair<int, int>> routes[MAX_SIZE + 1];
int distances[MAX_SIZE + 1];
void calcDist() {
for (int i = 1; i <= noVillages; ++i) {
distances[i] = INT_MAX;
}
distances[start] = 0;
queue<pair<int, int>> villages;
villages.push(make_pair(start, 0)); // dist cur = 0;
int ok = 0;
while (!villages.empty()) {
int actVillage = villages.front().first;
int actDistance = villages.front().second;
if (actVillage == finish) {
fout << actDistance;
return;
}
villages.pop();
for (pair<int, int> next : routes[actVillage]) {
int nextVillage = next.first;
int nextDistance = next.second;
if (distances[nextVillage] == INT_MAX) {
int totalDist = actDistance;
if (actVillage < nextVillage) { // -> pozitiv
totalDist += nextDistance;
} else {
totalDist -= nextDistance;
}
distances[nextVillage] = totalDist;
villages.push(make_pair(nextVillage, totalDist));
}
}
}
fout << -1;
}
int main() {
ios_base::sync_with_stdio(false);
fin >> noVillages >> noRoutes >> start >> finish;
for (int i = 1; i <= noRoutes; ++i) {
int left, right, dist;
fin >> left >> right >> dist;
routes[left].push_back(make_pair(right, dist));
routes[right].push_back(make_pair(left, dist));
}
calcDist();
}