Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
5.2 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;

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>> 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 * -1;
        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 * -1, newNode));
            }
        }
    }
    return -1;
}

void updateIfChange(int groupId, int s, int d) {
    int dist = getDistance(groupId, s, d);
    if (dist != -1 && dist != distMatrix[groupId * 3 + s][groupId * 3 + d]) {
        isGroupUpdated = true;
        distMatrix[groupId * 3 + s][groupId * 3 + d] = dist;
        distMatrix[groupId * 3 + d][groupId * 3 + s] = dist;
    }
}

bool updateGroup(int groupId) {
    isGroupUpdated = false;

    updateIfChange(groupId, 1, 2);
    updateIfChange(groupId, 1, 3);
    updateIfChange(groupId, 2, 3);

    return isGroupUpdated;
}

void updateResult() {
    res[1][2] = calculateTime(1, 2);
    res[2][1] = res[1][2];

    res[1][3] = calculateTime(1, 3);
    res[3][1] = res[1][3];

    res[2][3] = calculateTime(2, 3);
    res[3][2] = res[2][3];
}

int groupId1, groupId2, nodeId1, nodeId2;

void updateGroupNodeId(int nodeA, int nodeB) {
    groupId1 = nodeA / 100;
    groupId2 = nodeB / 100;
    nodeId1 = nodeA % 100;
    nodeId2 = nodeB % 100;
}

void init(int N, int K, int nodeA[], int nodeB[], int Time[]) {
    gv_N = N;
    gv_K = K;
    memset(distMatrix, 0, sizeof(distMatrix));
    for (int i = 0; i < 305; i++) {
        for (int j = 0; j < 35; j++) {
            graph[i][j].clear();
        }
    }

    for (int i = 0; i < gv_K; i++) {
        updateGroupNodeId(nodeA[i], nodeB[i]);

        if (groupId1 == groupId2) {
            graph[groupId1][nodeId1].push_back(make_pair(Time[i], nodeId2));
            graph[groupId1][nodeId2].push_back(make_pair(Time[i], nodeId1));
        } else {
            distMatrix[groupId1 * 3 + nodeId1][groupId2 * 3 + nodeId2] = Time[i];
            distMatrix[groupId2 * 3 + nodeId2][groupId1 * 3 + nodeId1] = Time[i];
        }
    }

    for (int i = 1; i <= gv_N; i++) {
        updateGroup(i);
    }
    updateResult();
}

void addLine(int nodeA, int nodeB, int Time) {
    updateGroupNodeId(nodeA, nodeB);

    if (groupId1 == groupId2) {
        graph[groupId1][nodeId1].push_back(make_pair(Time, nodeId2));
        graph[groupId1][nodeId2].push_back(make_pair(Time, nodeId1));

        if (updateGroup(groupId1)) {
            updateResult();
        }
    } else {
        distMatrix[groupId1 * 3 + nodeId1][groupId2 * 3 + nodeId2] = Time;
        distMatrix[groupId2 * 3 + nodeId2][groupId1 * 3 + nodeId1] = Time;
        updateResult();
    }
}

void remove(int groupId, int nodeA, int nodeB) {
    for (auto itr = graph[groupId][nodeA].begin(); itr != graph[groupId][nodeA].end(); itr++) {
        if ((*itr).second == nodeB) {
            graph[groupId][nodeA].erase(itr);
            break;
        }
    }
}

void removeLine(int nodeA, int nodeB) {
    updateGroupNodeId(nodeA, nodeB);

    if (groupId1 == groupId2) {
        remove(groupId1, nodeId1, nodeId2);
        remove(groupId1, nodeId2, nodeId1);

        if (updateGroup(groupId1)) {
            updateResult();
        }
    } else {
        distMatrix[groupId1 * 3 + nodeId1][groupId2 * 3 + nodeId2] = 0;
        distMatrix[groupId2 * 3 + nodeId2][groupId1 * 3 + nodeId1] = 0;
        updateResult();
    }
}

int calculateTime(int nodeA, int nodeB) {
    int ans = -1;
    priority_queue<pair<int, int>> 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 * -1;
        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 * -1, i));
                }
            }
        }
    }
    return ans;
}

int checkTime(int nodeA, int nodeB) {
    return res[nodeA][nodeB];
}
Leave a Comment