Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.5 kB
3
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];
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({ 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][node1].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({ newNode, newTime });
			}
		}
	}
	return -1;
}

void updateGroup(int gr) {
	int t1 = getTime2Node(gr, 1, 2);
	int t2 = getTime2Node(gr, 2, 3);
	int t3 = getTime2Node(gr, 3, 1);
	timeMainNode[gr * 3 + 1][gr * 3 + 2] = timeMainNode[gr * 3 + 2][gr * 3 + 1] = t1;
	timeMainNode[gr * 3 + 2][gr * 3 + 3] = timeMainNode[gr * 3 + 3][gr * 3 + 2] = t2;
	timeMainNode[gr * 3 + 1][gr * 3 + 3] = timeMainNode[gr * 3 + 3][gr * 3 + 1] = t3;
}

void updateTimeAllGr() {
	for (int gr = 1; gr <= numGr; gr++) {
		updateGroup(gr);
	}
}
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({ Node2, Time[i] });
			graph[gr1][Node2].push_back({ Node1, Time[i] });
		}
		else {
			timeMainNode[gr1 * 3 + Node1][gr2 * 3 + Node2] = Time[i];
			timeMainNode[gr2 * 3 + Node2][gr1 * 3 + Node1] = Time[i];
		}
	}
	updateTimeAllGr();
}

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

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

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({ 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({ i, best_time });
					time[i] = best_time;
				}
			}
		}
	}
	return -1;
}

int checkTime(int nodeA, int nodeB) {

}
Leave a Comment