Skip to content

Instantly share code, notes, and snippets.

@Aqendo
Created November 8, 2022 17:16
Show Gist options
  • Save Aqendo/76dd17e2814c1d27e0f370f548042f26 to your computer and use it in GitHub Desktop.
Save Aqendo/76dd17e2814c1d27e0f370f548042f26 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
/*
* input:
* <size of table>
* <start point (from 1 to size)
* <end point (from 1 to size)>
* <value if you SHOULD drive there (from 1 to size) else 0>
* <value if you SHOULDN'T drive there (from 1 to size) else 0>
* <table, example:
* 0 3 4 0 0 1
* 3 0 1 4 0 0
* 5 1 0 2 0 9
* 0 4 2 0 3 6
* 0 0 0 3 0 4
* 1 0 9 6 4 0>
*
*
* THIS PROGRAM IS NOT MEANT TO BE 100% ACCURATE. DO NOT TRUST ITS VALUES. IT MIGHT BE WRONG IN SOME CASES (MAYBE).
* P.S. who steal this program is a bad person)))
*/
int solve(const vector<vector<int>>& array, int end, vector<int>& used, int& result, int target, int no_target) {
for (int i = 0; i < array[used[used.size()-1]].size(); i++) {
if(array[used[used.size()-1]][i] == 0) continue;
bool is_already_used = false;
for (int j = 0; j < used.size(); j++) {
if (used[j] == i) is_already_used = true;
}
if (is_already_used) continue;
used.push_back(i);
if (i == end) {
int sum = 0;
if (target != -1) {
bool target_was_here = false;
for (int j = 0; j < used.size(); j++) {
if (used[j] == target) {
target_was_here = true;
}
}
if (!target_was_here) {
used.pop_back();
continue;
}
}
else if (no_target != -1) {
bool target_was_here = false;
for (int j = 0; j < used.size(); j++) {
if (used[j] == no_target) target_was_here = true;
}
if (target_was_here) {
used.pop_back();
continue;
}
}
for (int j = 1; j < used.size(); j++) {
sum += array[used[j-1]][used[j]];
}
if (sum < result) result=sum;
} else {
solve(array, end, used, result, target, no_target);
}
used.pop_back();
}
}
int main() {
int size, start, end, target, no_target;
cin >> size >> start >> end >> target >> no_target;
start--;end--;target--;no_target--;
vector<vector<int>> table(size, vector<int>(size));
for(int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cin >> table[i][j];
}
}
int result = 9999; // maximum limit
vector<int> used;
used.push_back(start);
solve(table, end, used, result, target, no_target);
cout << result;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment