Untitled
unknown
plain_text
a month ago
4.1 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 tran ke luu dist giua cac Main Node in Group int numGr, k; int groupId1, groupId2, nodeId1, nodeId2; int res[4][4]; int distance(int groupId, int start, int end) { // Min Dist from node Start to node End in same group int dist[30]; // dist from start to other node for(int i=0; i<=30; i++) dist[i] = MAX; priority_queue<pair<int, int>>pq; pq.push(make_pair(start, 0)); dist[start] = 0; while(!pq.empty()) { pair<int, int> temp = pq.top(); pq.pop(); int temp_node = temp.first; int temp_dist = temp.second; 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(newNode, newDist)); } } } 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, 3, 2); } 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<950; i++) { for(int j=0; j<950; j++) { distGroup[i][j] = 0; graph[i][j].clear(); } } for(int i=0; i<k; i++) { // convertGroupId_NodeId(nodeA[i], nodeB[i]); if(groupId1 == groupId2) {// Same group graph[groupId1][nodeId1].push_back(make_pair(nodeId2, Time[i])); graph[groupId1][nodeId2].push_back(make_pair(nodeId1, Time[i])); } else { // Different group 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) { // Same Group 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) { for(auto it = graph[groupId][nodeA].begin(); it!=graph[groupId][nodeA].end(); it++) { if((*it).first == nodeB) { graph[groupId][nodeA].erase(it); } } } 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) { int ans = -1; priority_queue<pair<int, int>>pq; int minDist[904]; for(int i=0; i<904; i++) minDist[i] = MAX; pq.push(make_pair(nodeA, 0)); minDist[nodeA] = 0; while(!pq.empty()) { pair<int, int>temp = pq.top(); pq.pop(); int cur_node = temp.first; int cur_dist = temp.second; if(cur_node == nodeB) { ans = cur_dist; return ans; } for(int i=0; i<904; i++) { if(!distGroup[cur_node][i]) { int best_dist = cur_dist + distGroup[cur_node][i]; if(best_dist < minDist[i]) { minDist[i] = best_dist; pq.push(make_pair(i, best_dist)); } } } } return ans; }
Leave a Comment