Advertisement
Guest User

CCC 2015 S4 : Convex Hull

a guest
May 22nd, 2015
2,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. /*
  2. Thanks to kobortor (Joey) for help!
  3.  
  4. CCC 2015 S4 : Convex Hull
  5.  
  6. Basically, the way to approach this problem is sort of a 3 Queue Dijkstra's (forgive me if I spelled the name wrong).
  7. What I did, is, create 2 classes that would respresent an Island, and a canal link between two islands. And from there,
  8. I would try to calculate the largest distance from A to B, with the hull height being above 0, duh.
  9.  
  10. Dealing with input is really straight forward, since it's a bi-directional graph, we want to make sure that our graph
  11. is traversable both ways, this is an important step.
  12.  
  13. For the algorithm, as said above, we're dealing with 3 queues (each stores the best possible distance, hull height, and the
  14. island that we're currently checking).
  15.  
  16. You want to get the maximum possible hull height OR the max possible distance, with respect to the hull being
  17. above 0 in height, then just push the maximum values to the queue.
  18.  
  19. Before printing the distance, check if we've reached the destination, if we haven't, print -1. We know that we haven't reached
  20. the destination when the shortest path to it is INFINITY (as labeled from the start)
  21.  
  22. And that's about it.
  23. */
  24.  
  25. #include <iostream>
  26. #include <vector>
  27. #include <queue>
  28. #include <climits>
  29.  
  30. using namespace std;
  31.  
  32. const int MAXN = 10010;
  33. typedef long long ll;
  34.  
  35. int convexHull, N, M, start, finish;
  36.  
  37. struct canal;
  38.  
  39. //all hail joey
  40.  
  41. struct island{
  42.     vector<canal> outgoing;
  43.     int shortestpath, hull;
  44.     island() {
  45.         shortestpath = INT_MAX; //who cares?
  46.         hull = -1;
  47.     }
  48. };
  49.  
  50. struct canal{
  51.     island* destination;
  52.     ll weight;
  53.     ll hulldamage;
  54.    
  55.     canal(island* dest, ll w, ll dmg){
  56.         destination = dest;
  57.         weight = w;
  58.         hulldamage = dmg;
  59.     }
  60. };
  61.  
  62. island islands[MAXN]; //safety
  63. int a, b, c, d;
  64.  
  65. int totalWeight, damage;
  66.  
  67. void Dijkstra(){
  68.     queue<island*> toCheck;
  69.     queue<int> distance;
  70.     queue<int> maxHull;
  71.  
  72.     islands[a].shortestpath = 0;
  73.    
  74.     distance.push(0);
  75.     toCheck.push(&islands[start]);
  76.     maxHull.push(convexHull);
  77.  
  78.     while(!toCheck.empty()){
  79.        
  80.         island* front = toCheck.front();
  81.         int dist = distance.front();
  82.         int height = maxHull.front();
  83.  
  84.         toCheck.pop();
  85.         maxHull.pop();
  86.         distance.pop();
  87.        
  88.         for(canal &c : front->outgoing){
  89.             int str = height - c.hulldamage;
  90.             int time = dist + c.weight;
  91.             island* destination = c.destination;
  92.             if((time < destination->shortestpath || str > destination->hull) && str > 0){
  93.                 destination->shortestpath = std::min(time, destination->shortestpath);
  94.                 destination->hull = std::max(destination->hull, str);
  95.                 toCheck.push(destination);
  96.                 maxHull.push(str);
  97.                 distance.push(time);
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. int main() {
  104.     scanf("%d%d%d", &convexHull, &N, &M);
  105.     while(M--){
  106.         scanf("%d%d%d%d", &a, &b, &c, &d);
  107.         islands[a].outgoing.emplace_back(canal(&islands[b], c, d));
  108.         islands[b].outgoing.emplace_back(canal(&islands[a], c, d));
  109.     }
  110.     scanf("%d%d", &start, &finish);
  111.     Dijkstra();
  112.     if(islands[finish].shortestpath == INT_MAX){ //broken..
  113.         printf("-1");
  114.     } else {
  115.         printf("%d", islands[finish].shortestpath);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement