Advertisement
nna42799

Untitled

Sep 25th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. using ull = unsigned long long;
  7. using ll = long long; // 1e18
  8.  
  9. bool debug=false;
  10.  
  11. const int TOTAL_NODE_COUNT = 10020;
  12.  
  13. ll nodeCoor[TOTAL_NODE_COUNT][2]; // server node stored behind normal node
  14.  
  15. ull connected[TOTAL_NODE_COUNT][TOTAL_NODE_COUNT]; // first idx lower then second idx
  16.  
  17. void connectNodes(ll firstIdx, ll secondIdx, ull weight){
  18.     connected[firstIdx][secondIdx] = weight;
  19.     connected[secondIdx][firstIdx] = weight;
  20.     return;
  21. }
  22.  
  23.  
  24. bool isConnected(ll firstIdx, ll secondIdx){
  25.     return connected[firstIdx][secondIdx] > 0;
  26. }
  27.  
  28. ull shortest[TOTAL_NODE_COUNT];
  29. bool confirmed[TOTAL_NODE_COUNT];
  30.  
  31. ull calcShortestRoute(ll nodeCount, ll serverCount){
  32.     ull allTypeNodeCount = nodeCount + serverCount;
  33.  
  34.     // confirm first node
  35.     shortest[0] = 0;
  36.     confirmed[0] = true;
  37.  
  38.     ll firstTimeUseZeroIdx = true;
  39.     while(true){
  40.         // if answer found, return
  41.         if(confirmed[nodeCount - 1]){
  42.             return shortest[nodeCount - 1];
  43.         }
  44.  
  45.         // select a node to search with in this round
  46.         ll nextCheckIdx = -1;
  47.  
  48.         for(int i = 1; i < allTypeNodeCount; ++i){
  49.             // special case
  50.             if(firstTimeUseZeroIdx){
  51.                 firstTimeUseZeroIdx = false;
  52.                 nextCheckIdx = 0;
  53.                 break;
  54.             }
  55.  
  56.             // not have a distance, or already confirmed
  57.             if(confirmed[i] || shortest[i] == 0){
  58.                 continue;
  59.             }
  60.             // if no nodes selected yet
  61.             if(nextCheckIdx == -1){
  62.                 nextCheckIdx = i;
  63.                 continue;
  64.             }
  65.             if(shortest[i] < shortest[nextCheckIdx]){
  66.                 nextCheckIdx = i;
  67.             }
  68.         }
  69.         // confirm the selected node
  70.         confirmed[nextCheckIdx] = true;
  71.         if(debug){
  72.             cout << "Next selected: " << nextCheckIdx << endl;
  73.         }
  74.  
  75.         // node selected, find all other nodes connected to
  76.         // then update shortest
  77.         for(int i = 1; i < allTypeNodeCount; ++i){
  78.             if(confirmed[i] || !isConnected(i, nextCheckIdx)){
  79.                 continue;
  80.             }
  81.             ull newDistance = connected[i][nextCheckIdx] + shortest[nextCheckIdx];
  82.             if(shortest[i] == 0 || shortest[i] > newDistance){
  83.                 shortest[i] = newDistance;
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89.  
  90. int main(){
  91.     // input metadata
  92.     ll nodeCount, serverCount;
  93.     cin >> nodeCount >> serverCount;
  94.  
  95.     // input node info
  96.     for(int i=0;i<nodeCount;++i){
  97.         cin >> nodeCoor[i][0] >> nodeCoor[i][1];
  98.     }
  99.  
  100.     // input server range info
  101.     ll tmpCenterPointX, tmpCenterPointY, tmpR, tmpWeight;
  102.     for(int i=0;i<serverCount;++i){
  103.         // input info
  104.         cin >> tmpCenterPointX >> tmpCenterPointY >> tmpR >> tmpWeight;
  105.  
  106.         // construct x and y range
  107.         ll xRangeLow, xRangeHigh, yRangeLow, yRangeHigh;
  108.         xRangeLow = tmpCenterPointX - tmpR;
  109.         xRangeHigh = tmpCenterPointX + tmpR;
  110.         yRangeLow = tmpCenterPointY - tmpR;
  111.         yRangeHigh = tmpCenterPointY + tmpR;
  112.  
  113.         // check which bunch of normal nodes are connected to this server
  114.         ll tmpCurNodeX, tmpCurNodeY;
  115.         for(int nnodeIdx=0;nnodeIdx < nodeCount;++nnodeIdx){
  116.             tmpCurNodeX = nodeCoor[nnodeIdx][0];
  117.             tmpCurNodeY = nodeCoor[nnodeIdx][1];
  118.             if(tmpCurNodeX >= xRangeLow && tmpCurNodeX <= xRangeHigh && tmpCurNodeY >= yRangeLow && tmpCurNodeY <= yRangeHigh){
  119.                 // connect this normal node and current inputing server node
  120.                 connectNodes(i + nodeCount, nnodeIdx, tmpWeight);
  121.                 if(debug){
  122.                     cout << "Connecting: " << nnodeIdx << " <--> " << i + nodeCount << endl;
  123.                 }
  124.             }
  125.         }
  126.     }
  127.  
  128.     cout << calcShortestRoute(nodeCount, serverCount) / 2;
  129.  
  130.     return 0;
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement