main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "instance.h"
  2. using namespace std;
  3. instance ins;
  4. pos drone_pos;
  5. int order_d(const order &o) {
  6. int d = -1;
  7. int poids = 0;
  8. int best_wh = -1;
  9. if(o.ord.empty())
  10. return 2000000000; // INF
  11. for(auto item : o.ord) {
  12. poids += item.second*ins.weights[item.first];
  13. }
  14. for(int i = 0; i < ins.W; i++) {
  15. bool ok = false;
  16. for(pair<int, int> item : o.ord) {
  17. if(ins.wh[i].disp[item.first] > 0) {
  18. ok = true;
  19. break;
  20. }
  21. }
  22. if(ok) {
  23. int d2 = drone_pos.dist2(ins.wh[i].p) + ins.wh[i].p.dist2(o.p);
  24. if(d < 0 || d2 < d) {
  25. d = d2;
  26. best_wh = i;
  27. }
  28. }
  29. }
  30. if(d < 0) {
  31. cerr << "! d < 0" << endl;
  32. return 2000000000; // INF
  33. }
  34. if(poids > ins.max_load)
  35. d += ins.col*ins.col + ins.row*ins.row;
  36. ins.orders[o.id].best_wh = best_wh; // (o is const)
  37. return d;
  38. }
  39. bool cmp_ord(const order &a, const order &b) {
  40. return order_d(a) < order_d(b);
  41. }
  42. class drone {
  43. public:
  44. drone(int id, pos p) : id(id), temps(0), p(p) {}
  45. int id;
  46. int temps;
  47. pos p;
  48. bool operator<(const drone &b) const {
  49. return temps > b.temps;
  50. }
  51. };
  52. void glouton(instance &ins) {
  53. int score = 0;
  54. priority_queue<drone> Q;
  55. for(int i = 0; i < ins.drones; i++)
  56. Q.push(drone(i, ins.wh[0].p));
  57. while(!Q.empty()) {
  58. drone dr = Q.top();
  59. Q.pop();
  60. drone_pos = dr.p;
  61. order &ord = *min_element(ins.orders.begin(), ins.orders.end(), cmp_ord);
  62. if(ord.ord.empty())
  63. break;
  64. assert(ord.best_wh >= 0);
  65. int wa = ord.best_wh;
  66. // augment dr.temps
  67. dr.temps += 1+ceil(sqrt(dr.p.dist2(ins.wh[wa].p)));
  68. dr.temps += 1+ceil(sqrt(ins.wh[wa].p.dist2(ord.p)));
  69. dr.p = ord.p;
  70. if(dr.temps > ins.T) {
  71. continue;
  72. }
  73. // planifie l'ordre
  74. int load = 0;
  75. vector<pair<int, int>> aprendre;
  76. for(pair<int, int> item : ord.ord) {
  77. assert(load <= ins.max_load);
  78. if(load == ins.max_load)
  79. break;
  80. int q = min(item.second, ins.wh[wa].disp[item.first]);
  81. q = min(q, (ins.max_load-load)/ins.weights[item.first]);
  82. if(q > 0) {
  83. aprendre.push_back(make_pair(item.first, q));
  84. load += ceil(q*ins.weights[item.first]);
  85. }
  86. }
  87. if(aprendre.empty())
  88. cerr << "! aprendre is empty" << endl;
  89. // pass order
  90. for(pair<int, int> item : aprendre) {
  91. // decrease order / stock
  92. ins.wh[wa].disp[item.first] -= item.second;
  93. ord.ord[item.first] -= item.second;
  94. if(!ord.ord[item.first])
  95. ord.ord.erase(item.first);
  96. cout << dr.id << " L " << wa << " " << item.first << " " << item.second << endl;
  97. }
  98. for(pair<int, int> item : aprendre) {
  99. cout << dr.id << " D " << ord.id << " " << item.first << " " << item.second << endl;
  100. }
  101. // push again drone
  102. Q.push(dr);
  103. // compute score
  104. if(ord.ord.empty())
  105. score += ceil(100.*(ins.T-dr.temps)/(double)ins.T);
  106. }
  107. cerr << "Score : " << score << endl;
  108. }
  109. int main() {
  110. int seed = time(NULL);
  111. srand(seed);
  112. cerr << "seed : " << seed << endl;
  113. //instance ins;
  114. glouton(ins);
  115. return 0;
  116. }