D_L3

test 7 - 2018-2019

Jan 8th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <unordered_map>
  8. #include <climits>
  9. using namespace std;
  10.  
  11. unordered_map<int, vector<pair<int, int>>> graph;
  12. unordered_map<int, int> mostCommon;
  13.  
  14.  
  15. void dfs(int currWeight, int currIdx, int k, int currK, vector<bool>& visited){
  16. if(currK == k){
  17. mostCommon[currWeight]++;
  18. return;
  19. }
  20. visited[currIdx] = true;
  21.  
  22. for (auto neighbour : graph[currIdx]) {
  23. int nextWeight = currWeight + neighbour.second;
  24. int nextIdx = neighbour.first;
  25. int nextK = currK + 1;
  26.  
  27. if (!visited[nextIdx])
  28. dfs(nextWeight, nextIdx, k, currK + 1, visited);
  29. }
  30. visited[currIdx] = false;
  31. }
  32.  
  33. void mostCommonFor(int v, int k, int numOfV) {
  34. vector<bool> visited(numOfV, false);
  35.  
  36. dfs(0, v, k, 0, visited);
  37. }
  38.  
  39. int main() {
  40. int v, e, x, y, w, k;
  41. cin >> v >> e;
  42. for (int i = 0; i < e; i++) {
  43. cin >> x >> y >> w;
  44. graph[x - 1].push_back({ y - 1, w });
  45. }
  46. cin >> k;
  47.  
  48. for (int i = 0; i < v; i++) {
  49. mostCommonFor(i, k, v);
  50. }
  51.  
  52. int result = -1;
  53. int resultVal = -1;
  54. mostCommon[-1] = 0;
  55.  
  56. for (auto res : mostCommon) {
  57. if (result < res.second || (result == res.second && res.first > resultVal)) {
  58. result = res.second;
  59. resultVal = res.first;
  60. }
  61. }
  62. cout << resultVal;
  63. return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment