Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <cstdlib>
- #define INF 2147483647
- using namespace std;
- /* Returns adjacent vector to node */
- vector<int> get_adjacent(vector<vector<int>> adj, int node) {
- vector<int> res;
- for (int i = 0; i < adj.size(); i++) {
- if (adj[node][i] >= 0) {
- res.push_back(i);
- }
- }
- return res;
- }
- /* Check if node is in vector */
- bool vector_find(vector<int> vec, int v) {
- for (int i = 0; i < vec.size(); i++) {
- if (vec[i] == v) return true;
- }
- return false;
- }
- int dijkstra(vector<vector<int>> adj, int start, int goal)
- {
- start--; goal--;
- int n = adj.size();
- vector<int> costs(n, INF);
- vector<bool> permanent(n, false);
- vector<int> parent(n, -1);
- vector<int> working;
- costs[start] = 0;
- permanent[start] = true;
- int current_node = start;
- do {
- /* Next node */
- int next_x = -1;
- /* Add adjacents to working vector */
- vector<int> adjacents = get_adjacent(adj, current_node);
- for (int i = 0; i < adjacents.size(); i++) {
- /* If nodes are already in working vector OR are permanent skip them */
- if (vector_find(working, adjacents[i]) || permanent[adjacents[i]]) continue;
- working.push_back(adjacents[i]);
- }
- /* Get cheaper and update cost */
- for (int i = 0; i < working.size(); i++) {
- int t_node = working[i];
- /* Skip obvious checks */
- if (t_node == current_node) continue;
- if (permanent[t_node]) continue;
- int t_node_parent;
- /* Get cost from current_node */
- int cost = adj[current_node][t_node];
- /* If there aren't any path choose the other parent */
- if (cost == -1) {
- t_node_parent = parent[t_node];
- cost = adj[t_node_parent][t_node];
- } else {
- t_node_parent = current_node;
- }
- /* Check if this parent way is cheaper than old */
- if (costs[t_node_parent] + cost < costs[t_node]) {
- /* If it is update */
- costs[t_node] = costs[t_node_parent] + cost;
- parent[t_node] = t_node_parent;
- }
- /* What node should we move to? */
- if (next_x == -1 || costs[t_node] < (costs[t_node_parent] + costs[next_x])) {
- next_x = t_node;
- }
- }
- /* We move to cheapest node */
- permanent[next_x] = true;
- current_node = next_x;
- } while (current_node != goal);
- return costs[goal];
- }
- int main() {
- int N, A, B; // Nodes, Cold Route, Hot Route
- ifstream input("input.txt", ios::in);
- input >> N >> A >> B;
- vector<vector<int>> adj(N);
- for (int i = 0; i < N; i++) {
- vector<int> row (N, -1);
- adj[i] = row;
- }
- for(int i = 0, s, d; i < A+B; i++) {
- input >> s >> d;
- // cout << s << " - " << d << endl;
- adj[s-1][d-1] = adj[d-1][s-1] = (i >= A ? 1 : 0);
- }
- /*
- cout << ' ';
- for (int i = 0; i < N; i++)
- cout << ' ' << (i+1);
- cout << endl;
- for (int i = 0; i < N; i++) {
- cout << (i+1);
- for (int j = 0; j < N; j++)
- cout << ' ' << adj[i][j];
- cout << endl;
- }
- */
- input.close();
- int r = dijkstra(adj, 1, N);
- cout << r;
- ofstream o("output.txt");
- o << r;
- o.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment