Untitled
unknown
c_cpp
a month ago
1.5 kB
4
Indexable
Never
#include <bits/stdc++.h> #define ll long long int #define ull unsigned long long int using namespace std; int n; vector<pair<int, int>> arr; ll sum = INT_MAX; int findDist(pair<int, int> a, pair<int, int> b) { return (abs(a.first - b.first) + abs(a.second - b.second)); } void func(int pos, ll cur, int cnt, vector<bool> sele) { if (cnt == n) { if (cur + findDist(arr[pos], arr[0]) < sum) { sum = cur + findDist(arr[pos], arr[0]); } return; } if (cur >= sum) return; for (int i = 0; i < n; i++) { if (i != pos && sele[i] == false) { sele[i] = true; func(i, cur + findDist(arr[pos], arr[i]), cnt + 1, sele); sele[i] = false; } } } void solve() { int row, col; cin >> row >> col; pair<int, int> start; cin >> start.first >> start.second; cin >> n; n++; arr.resize(n); arr[0] = start; for (int i = 1; i < n; i++) { cin >> arr[i].first >> arr[i].second; } vector<bool> sele(n, false); sele[0] = true; func(0, 0, 1, sele); cout << "The shortest path has length " << sum << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll tc; cin >> tc; while (tc--) { solve(); } return 0; }
Leave a Comment