Guest User

Untitled

a guest
Sep 1st, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cstdlib>
  5.  
  6. #define INF 2147483647
  7.  
  8. using namespace std;
  9.  
  10. /* Returns adjacent vector to node */
  11. vector<int> get_adjacent(vector<vector<int>> adj, int node) {
  12.     vector<int> res;
  13.     for (int i = 0; i < adj.size(); i++) {
  14.         if (adj[node][i] >= 0) {
  15.             res.push_back(i);
  16.         }
  17.     }
  18.     return res;
  19. }
  20.  
  21. /* Check if node is in vector */
  22. bool vector_find(vector<int> vec, int v) {
  23.     for (int i = 0; i < vec.size(); i++) {
  24.         if (vec[i] == v) return true;
  25.     }
  26.     return false;
  27. }
  28.  
  29. int dijkstra(vector<vector<int>> adj, int start, int goal)
  30. {
  31.     start--; goal--;
  32.  
  33.     int n = adj.size();
  34.     vector<int> costs(n, INF);
  35.     vector<bool> permanent(n, false);
  36.     vector<int> parent(n, -1);
  37.     vector<int> working;
  38.  
  39.     costs[start] = 0;
  40.     permanent[start] = true;
  41.     int current_node = start;
  42.  
  43.     do {
  44.         /* Next node */
  45.         int next_x = -1;
  46.  
  47.         /* Add adjacents to working vector */
  48.         vector<int> adjacents = get_adjacent(adj, current_node);
  49.         for (int i = 0; i < adjacents.size(); i++) {
  50.             /* If nodes are already in working vector OR are permanent skip them */
  51.             if (vector_find(working, adjacents[i]) || permanent[adjacents[i]]) continue;
  52.             working.push_back(adjacents[i]);
  53.         }
  54.  
  55.         /* Get cheaper and update cost */
  56.         for (int i = 0; i < working.size(); i++) {
  57.             int t_node = working[i];
  58.             /* Skip obvious checks */
  59.             if (t_node == current_node) continue;
  60.             if (permanent[t_node]) continue;
  61.  
  62.             int t_node_parent;
  63.  
  64.             /* Get cost from current_node */
  65.             int cost = adj[current_node][t_node];
  66.  
  67.             /* If there aren't any path choose the other parent */
  68.             if (cost == -1) {
  69.                 t_node_parent = parent[t_node];
  70.                 cost = adj[t_node_parent][t_node];
  71.             } else {
  72.                 t_node_parent = current_node;
  73.             }
  74.             /* Check if this parent way is cheaper than old */
  75.             if (costs[t_node_parent] + cost < costs[t_node]) {
  76.                 /* If it is update */
  77.                 costs[t_node] = costs[t_node_parent] + cost;
  78.                 parent[t_node] = t_node_parent;
  79.             }
  80.             /* What node should we move to? */
  81.             if (next_x == -1 || costs[t_node] < (costs[t_node_parent] + costs[next_x])) {
  82.                 next_x = t_node;
  83.             }
  84.         }
  85.  
  86.         /* We move to cheapest node */
  87.         permanent[next_x] = true;
  88.         current_node = next_x;
  89.  
  90.     } while (current_node != goal);
  91.  
  92.     return costs[goal];
  93. }
  94.  
  95. int main() {
  96.     int N, A, B; // Nodes, Cold Route, Hot Route
  97.  
  98.     ifstream input("input.txt", ios::in);
  99.     input >> N >> A >> B;
  100.  
  101.     vector<vector<int>> adj(N);
  102.     for (int i = 0; i < N; i++) {
  103.         vector<int> row (N, -1);
  104.         adj[i] = row;
  105.     }
  106.  
  107.     for(int i = 0, s, d; i < A+B; i++) {
  108.         input >> s >> d;
  109.         // cout << s << " - " << d << endl;
  110.         adj[s-1][d-1] = adj[d-1][s-1] = (i >= A ? 1 : 0);
  111.     }
  112.  
  113.     /*
  114.     cout << ' ';
  115.     for (int i = 0; i < N; i++)
  116.         cout << ' ' << (i+1);
  117.     cout << endl;
  118.     for (int i = 0; i < N; i++) {
  119.         cout << (i+1);
  120.         for (int j = 0; j < N; j++)
  121.             cout << ' ' << adj[i][j];
  122.         cout << endl;
  123.     }
  124.     */
  125.  
  126.  
  127.     input.close();
  128.     int r = dijkstra(adj, 1, N);
  129.     cout << r;
  130.  
  131.     ofstream o("output.txt");
  132.     o << r;
  133.     o.close();
  134.  
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment