Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
2.4 kB
2
Indexable
Never
#include<iostream>
#include<vector>
#include<queue>
#include<climits>
#include<cstring>

using namespace std;

#define MAX 9999999

int calculateTime(int, int);

vector <pair<int, int>> graph[305][35]; 
int distMatrix[905][905]; 
int gv_N;
int gv_K;
int res[4][4];

bool isGroupUpdated = false;

// Custom comparator for the priority queue
struct CompareDistance {
    bool operator()(pair<int, int> const& p1, pair<int, int> const& p2) {
        // Compare based on distance (first element of the pair)
        return p1.first > p2.first;
    }
};

int getDistance(int groupId, int src, int dest) {
    int minTime[32];
    for (int i = 0; i < 32; i++) minTime[i] = INT_MAX;

    priority_queue<pair<int, int>, vector<pair<int, int>>, CompareDistance> pq;
    pq.push(make_pair(0, src));
    minTime[src] = 0;

    while (!pq.empty()) {
        pair<int, int> temp = pq.top();
        pq.pop();

        int dist = temp.first;
        int node = temp.second;

        if (node == dest) return dist;

        for (int i = 0; i < graph[groupId][node].size(); i++) {
            int newNode = graph[groupId][node][i].second;
            int newDist = graph[groupId][node][i].first + dist;

            if (newDist < minTime[newNode]) {
                minTime[newNode] = newDist;
                pq.push(make_pair(newDist, newNode));
            }
        }
    }
    return -1;
}

// ... (rest of the code remains the same until calculateTime function)

int calculateTime(int nodeA, int nodeB) {
    int ans = -1;
    priority_queue<pair<int, int>, vector<pair<int, int>>, CompareDistance> pq;

    int minTime[905];
    for (int i = 0; i < 905; i++) minTime[i] = MAX;

    pq.push(make_pair(0, nodeA));
    minTime[nodeA] = 0;

    while (!pq.empty()) {
        int cur_node = pq.top().second;
        int cur_time = pq.top().first;
        pq.pop();

        if (cur_node == nodeB) {
            ans = cur_time;
            return ans;
        }

        for (int i = 0; i < 905; i++) {
            if (distMatrix[cur_node][i] != 0) {
                int tot_time = cur_time + distMatrix[cur_node][i];
                if (tot_time < minTime[i]) {
                    minTime[i] = tot_time;
                    pq.push(make_pair(tot_time, i));
                }
            }
        }
    }
    return ans;
}

// ... (rest of the code remains the same)
Leave a Comment