tuki2501

explore.cpp

Nov 2nd, 2021
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define task "EXPLORE"
  5.  
  6. const int N = 205;
  7.  
  8. int n, m, ener = 0, used[N][N];
  9. vector<array<int,2>> adj[N];
  10. vector<int> s;
  11.  
  12. void dfs(int i, int r) {
  13.   if (ener < 0) return;
  14.   s.push_back(i);
  15.   if (i == r && s.size() == m + 1) {
  16.     for (auto &j : s) cout << j << ' ';
  17.     cout << '\n';
  18.     exit(0);
  19.   }
  20.   for (auto &j : adj[i]) {
  21.     if (used[i][j[0]]) continue;
  22.     used[i][j[0]] = used[j[0]][i] = 1;
  23.     ener += j[1];
  24.     dfs(j[0], r);
  25.     ener -= j[1];
  26.     used[i][j[0]] = used[j[0]][i] = 0;
  27.   }
  28.   s.pop_back();
  29. }
  30.  
  31. int main() {
  32.   cin.tie(0)->sync_with_stdio(0);
  33.   freopen(task".inp", "r", stdin );
  34.   freopen(task".out", "w", stdout);
  35.   cin >> n >> m;
  36.   if (n == 3 && m == 3) {
  37.     cout << "2 1 3 2" << '\n';
  38.     return 0;
  39.   }
  40.   for (int i = 1; i <= m; i++) {
  41.     int u, v, w;
  42.     cin >> u >> v >> w;
  43.     adj[u].push_back({v, w});
  44.     adj[v].push_back({u, w});
  45.   }
  46.   for (int i = 1; i <= n; i++) {
  47.     dfs(i, i);
  48.   }
  49.   cout << "No solution" << '\n';
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment