Untitled
unknown
plain_text
a month ago
4.0 kB
3
Indexable
Never
#include<iostream> #include<vector> #include<queue> #include<climits> #include<cstring> #define MAX 999999999 using namespace std; vector<pair<int, int>> graph[500][500]; int distGroup[1000][1000]; // Ma trận kề lưu dist giữa các Main Node in Group int numGr, k; int groupId1, groupId2, nodeId1, nodeId2; int distance(int groupId, int start, int end) { int dist[30]; // dist from start to other node for(int i = 0; i < 30; i++) dist[i] = MAX; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.push(make_pair(0, start)); dist[start] = 0; while(!pq.empty()) { pair<int, int> temp = pq.top(); pq.pop(); int temp_node = temp.second; int temp_dist = temp.first; if(temp_node == end) { return temp_dist; } for(int i = 0; i < graph[groupId][temp_node].size(); i++) { int newNode = graph[groupId][temp_node][i].first; int newDist = graph[groupId][temp_node][i].second + temp_dist; if(newDist < dist[newNode]) { dist[newNode] = newDist; pq.push(make_pair(newDist, newNode)); } } } return -1; } void updateMainNode(int groupId, int start, int end) { int newDistance = distance(groupId, start, end); if(newDistance != -1 && newDistance != distGroup[groupId * 3 + start][groupId * 3 + end]) { distGroup[groupId * 3 + start][groupId * 3 + end] = newDistance; distGroup[groupId * 3 + end][groupId * 3 + start] = newDistance; } } void updateGroup(int groupId) { updateMainNode(groupId, 1, 2); updateMainNode(groupId, 1, 3); updateMainNode(groupId, 2, 3); } void convertGroupId_NodeId(int nodeA, int nodeB) { groupId1 = nodeA / 100; nodeId1 = nodeA % 100; groupId2 = nodeB / 100; nodeId2 = nodeB % 100; } void init(int N, int K, int nodeA[], int nodeB[], int Time[]) { numGr = N; k = K; for(int i = 0; i < 1000; i++) { for(int j = 0; j < 1000; j++) { distGroup[i][j] = (i == j) ? 0 : MAX; } } for(int i = 0; i < k; i++) { convertGroupId_NodeId(nodeA[i], nodeB[i]); if(groupId1 == groupId2) { graph[groupId1][nodeId1].push_back(make_pair(nodeId2, Time[i])); graph[groupId1][nodeId2].push_back(make_pair(nodeId1, Time[i])); } else { distGroup[groupId1 * 3 + nodeId1][groupId2 * 3 + nodeId2] = Time[i]; distGroup[groupId2 * 3 + nodeId2][groupId1 * 3 + nodeId1] = Time[i]; } } for(int i = 1; i <= numGr; i++) { updateGroup(i); } } void addLine(int nodeA, int nodeB, int Time) { convertGroupId_NodeId(nodeA, nodeB); if(groupId1 == groupId2) { graph[groupId1][nodeId1].push_back(make_pair(nodeId2, Time)); graph[groupId1][nodeId2].push_back(make_pair(nodeId1, Time)); } else { distGroup[groupId1 * 3 + nodeId1][groupId2 * 3 + nodeId2] = Time; distGroup[groupId2 * 3 + nodeId2][groupId1 * 3 + nodeId1] = Time; } } void remove(int groupId, int nodeA, int nodeB) { graph[groupId][nodeA].erase(remove_if(graph[groupId][nodeA].begin(), graph[groupId][nodeA].end(), [nodeB](const pair<int, int>& p) { return p.first == nodeB; }), graph[groupId][nodeA].end()); } void removeLine(int nodeA, int nodeB) { convertGroupId_NodeId(nodeA, nodeB); if(groupId1 == groupId2) { remove(groupId1, nodeId1, nodeId2); remove(groupId1, nodeId2, nodeId1); } else { distGroup[groupId1 * 3 + nodeId1][groupId2 * 3 + nodeId2] = 0; distGroup[groupId2 * 3 + nodeId2][groupId1 * 3 + nodeId1] = 0; } } int checkTime(int nodeA, int nodeB) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; int minDist[904]; for(int i = 0; i < 904; i++) minDist[i] = MAX; pq.push(make_pair(0, nodeA)); minDist[nodeA] = 0; while(!pq.empty()) { pair<int, int> temp = pq.top(); pq.pop(); int cur_node = temp.second; int cur_dist = temp.first; if(cur_node == nodeB) { return cur_dist; } for(int i = 0; i < 904; i++) { if(distGroup[cur_node][i] != 0) { int best_dist = cur_dist + distGroup[cur_node][i]; if(best_dist < minDist[i]) { minDist[i] = best_dist; pq.push(make_pair(best_dist, i)); } } } } return -1; }
Leave a Comment