Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.6 kB
2
Indexable
Never
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#define MAX 999999
using namespace std;

vector<pair<int, int>> graph[301][31];
int timeMainNode[301][301];
int numGr, numLine;
int res[4][4];
bool isUpdate;
int calculateRootNode(int nodeA, int nodeB);
struct cmp {
    bool operator()(pair<int, int> &a, pair<int, int> &b) {
        return a.second > b.second;
    }
};

int getTime2Node(int grId, int node1, int node2) {
    int res = -1;
    int time[31];
    for (int i = 0; i < 31; i++) time[i] = MAX;
    time[node1] = 0;
    priority_queue<pair<int, int>,vector<pair<int, int>>, cmp> pq;
    pq.push(make_pair(node1, 0));
    while (!pq.empty()) {
        pair<int, int> temp = pq.top();
        pq.pop();
        int cur_node = temp.first;
        int cur_time = temp.second;
        if (cur_node == node2) return cur_time;
        for (int i = 0; i < graph[grId][cur_node].size(); i++) {
            int newNode = graph[grId][node1][i].first;
            int newTime = graph[grId][node1][i].second + cur_time;
            if (newTime < time[newNode]) {
                time[newNode] = newTime;
                pq.push(make_pair(newNode, newTime));
            }
        }
    }
    return -1;
}

void updateCupleNode(int gr, int node1, int node2) {
    isUpdate = false;
    int t = getTime2Node(gr, node1, node2);
    if(t!=-1 && t!=timeMainNode[gr * 3 + node1][gr * 3 + node2]) {
        isUpdate = true;
        timeMainNode[gr * 3 + node1][gr * 3 + node2] = t;
        timeMainNode[gr * 3 + node2][gr * 3 + node1] = t;
    }
}

bool updateTimeGr(int gr) {
    isUpdate = false;
    updateCupleNode(gr, 1,2);
    updateCupleNode(gr, 2,3);
    updateCupleNode(gr, 3,1);
    return isUpdate;
}

void updateResult() {
    res[1][2] = res[2][1] = calculateRootNode(1, 2);
    res[2][3] = res[3][2] = calculateRootNode(2, 3);
    res[1][3] = res[3][1] = calculateRootNode(1, 3);
}

int gr1, gr2, Node1, Node2;
void convertNode(int nodeA, int nodeB) {
    gr1 = nodeA / 100;
    Node1 = nodeA % 100;
    gr2 = nodeB / 100;
    Node2 = nodeB % 100;
}

void init(int N, int K, int nodeA[], int nodeB[], int Time[]) {
    numGr = N;
    numLine = K;
    for (int i = 0; i < K; i++) {
        convertNode(nodeA[i], nodeB[i]);
        if (gr1 == gr2) {
            graph[gr1][Node1].push_back(make_pair( Node2, Time[i]));
            graph[gr1][Node2].push_back(make_pair(Node1, Time[i]));
        }
        else {
            timeMainNode[gr1 * 3 + Node1][gr2 * 3 + Node2] = Time[i];
            timeMainNode[gr2 * 3 + Node2][gr1 * 3 + Node1] = Time[i];
        }
    }
    for(int i=1; i<=numGr; i++) {
        updateTimeGr(i);
    }
    updateResult();
}

void addLine(int nodeA, int nodeB, int Time) {
    convertNode(nodeA, nodeB);
    if (gr1 == gr2) {
        graph[gr1][Node1].push_back(make_pair(Node2, Time));
        graph[gr1][Node2].push_back(make_pair(Node1, Time));
        if(updateTimeGr(gr1)) updateResult();
    }
    else {
        timeMainNode[gr1 * 3 + Node1][gr2 * 3 + Node2] = Time;
        timeMainNode[gr2 * 3 + Node2][gr1 * 3 + Node1] = Time;
        updateResult();
    }
}

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

void removeLine(int nodeA, int nodeB) {
    convertNode(nodeA, nodeB);
    if(gr1==gr2) {
        remove(gr1, Node1, Node2);
        remove(gr1, Node2, Node1);
        if(updateTimeGr(gr1)) updateResult();
        
    } else {
        timeMainNode[gr1*3+Node1][gr2*3+Node2] = 0;
        timeMainNode[gr1*3+Node2][gr2*3+Node1] = 0;
        updateResult();
    }
}

int calculateRootNode(int nodeA, int nodeB) {
    int ans = -1;
    priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
    int time[905];
    for (int i = 0; i < 905; i++) time[i] = MAX;
    time[nodeA] = 0;
    pq.push(make_pair(nodeA, 0));
    while (!pq.empty()) {
        pair<int, int> temp = pq.top();
        pq.pop();
        int cur_node = temp.first;
        int cur_time = temp.second;
        if (cur_node == nodeB) return cur_time;
        for (int i = 0; i < 905; i++) {
            if (timeMainNode[cur_node][i]) {
                int best_time = timeMainNode[cur_node][i] + cur_time;
                if (best_time < time[i]) {
                    pq.push(make_pair(i, best_time));
                    time[i] = best_time;
                }
            }
        }
    }
    return -1;
}

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