Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using std::cin;
- using std::cout;
- using std::endl;
- using ull = unsigned long long;
- using ll = long long; // 1e18
- bool debug=false;
- const int TOTAL_NODE_COUNT = 10020;
- ll nodeCoor[TOTAL_NODE_COUNT][2]; // server node stored behind normal node
- ull connected[TOTAL_NODE_COUNT][TOTAL_NODE_COUNT]; // first idx lower then second idx
- void connectNodes(ll firstIdx, ll secondIdx, ull weight){
- connected[firstIdx][secondIdx] = weight;
- connected[secondIdx][firstIdx] = weight;
- return;
- }
- bool isConnected(ll firstIdx, ll secondIdx){
- return connected[firstIdx][secondIdx] > 0;
- }
- ull shortest[TOTAL_NODE_COUNT];
- bool confirmed[TOTAL_NODE_COUNT];
- ull calcShortestRoute(ll nodeCount, ll serverCount){
- ull allTypeNodeCount = nodeCount + serverCount;
- // confirm first node
- shortest[0] = 0;
- confirmed[0] = true;
- ll firstTimeUseZeroIdx = true;
- while(true){
- // if answer found, return
- if(confirmed[nodeCount - 1]){
- return shortest[nodeCount - 1];
- }
- // select a node to search with in this round
- ll nextCheckIdx = -1;
- for(int i = 1; i < allTypeNodeCount; ++i){
- // special case
- if(firstTimeUseZeroIdx){
- firstTimeUseZeroIdx = false;
- nextCheckIdx = 0;
- break;
- }
- // not have a distance, or already confirmed
- if(confirmed[i] || shortest[i] == 0){
- continue;
- }
- // if no nodes selected yet
- if(nextCheckIdx == -1){
- nextCheckIdx = i;
- continue;
- }
- if(shortest[i] < shortest[nextCheckIdx]){
- nextCheckIdx = i;
- }
- }
- // confirm the selected node
- confirmed[nextCheckIdx] = true;
- if(debug){
- cout << "Next selected: " << nextCheckIdx << endl;
- }
- // node selected, find all other nodes connected to
- // then update shortest
- for(int i = 1; i < allTypeNodeCount; ++i){
- if(confirmed[i] || !isConnected(i, nextCheckIdx)){
- continue;
- }
- ull newDistance = connected[i][nextCheckIdx] + shortest[nextCheckIdx];
- if(shortest[i] == 0 || shortest[i] > newDistance){
- shortest[i] = newDistance;
- }
- }
- }
- }
- int main(){
- // input metadata
- ll nodeCount, serverCount;
- cin >> nodeCount >> serverCount;
- // input node info
- for(int i=0;i<nodeCount;++i){
- cin >> nodeCoor[i][0] >> nodeCoor[i][1];
- }
- // input server range info
- ll tmpCenterPointX, tmpCenterPointY, tmpR, tmpWeight;
- for(int i=0;i<serverCount;++i){
- // input info
- cin >> tmpCenterPointX >> tmpCenterPointY >> tmpR >> tmpWeight;
- // construct x and y range
- ll xRangeLow, xRangeHigh, yRangeLow, yRangeHigh;
- xRangeLow = tmpCenterPointX - tmpR;
- xRangeHigh = tmpCenterPointX + tmpR;
- yRangeLow = tmpCenterPointY - tmpR;
- yRangeHigh = tmpCenterPointY + tmpR;
- // check which bunch of normal nodes are connected to this server
- ll tmpCurNodeX, tmpCurNodeY;
- for(int nnodeIdx=0;nnodeIdx < nodeCount;++nnodeIdx){
- tmpCurNodeX = nodeCoor[nnodeIdx][0];
- tmpCurNodeY = nodeCoor[nnodeIdx][1];
- if(tmpCurNodeX >= xRangeLow && tmpCurNodeX <= xRangeHigh && tmpCurNodeY >= yRangeLow && tmpCurNodeY <= yRangeHigh){
- // connect this normal node and current inputing server node
- connectNodes(i + nodeCount, nnodeIdx, tmpWeight);
- if(debug){
- cout << "Connecting: " << nnodeIdx << " <--> " << i + nodeCount << endl;
- }
- }
- }
- }
- cout << calcShortestRoute(nodeCount, serverCount) / 2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement