# public Th0ar /Elevator-Emulator

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Algorithm Analysis.pdf in /Docs and simple notes in costflow.cpp

 `@@ -23,13 +23,13 @@` 23 23 ` * 构图做最小费用最大流，以保证在当前情况下满足最多数量的order并且总代价最小` 24 24 ` */` 25 25 ` ` 26 `-void addEdge(int a, int b, int f, int c)` 26 `+void addEdge(int a, int b, int f, int c) //加边函数，构图时用。a到b添加流量为f的，费用是c的边。` 27 27 ` {` 28 28 ` flow[a][b] = f, flow[b][a] = 0;` 29 29 ` cost[a][b] = c, cost[b][a] = -c;` 30 30 ` }` 31 31 ` ` 32 `-int BytheWay(Elevator *ele)` 32 `+int BytheWay(Elevator *ele) // 判断电梯是不是空闲。` 33 33 ` {` 34 34 ` if (ele->Get_nPassenger() == Elevator::Get_capacity())` 35 35 ` return 0;` `@@ -38,7 +38,7 @@ int BytheWay(Elevator *ele)` 38 38 ` else return 0;` 39 39 ` }` 40 40 ` ` 41 `-int spfa()` 41 `+int spfa() // 费用流中求最小费用权路径。用spfa实现以加速。` 42 42 ` {` 43 43 ` memset(dist, -1, sizeof(dist));` 44 44 ` memset(inq, 0, sizeof(inq));` `@@ -70,7 +70,7 @@ void Controller::CostFLow()` 70 70 ` int idx_order, idx_ele;` 71 71 ` idx_order = 0;` 72 72 ` for (OrderIter iter = order_s; iter != order_e; ++ iter)` 73 `- addEdge(S, ++ idx_order, 1, 0);` 73 `+ addEdge(S, ++ idx_order, 1, 0); // 每个order和S连边` 74 74 ` idx_order = 0;` 75 75 ` for (OrderIter iter = order_s; iter != order_e; ++ iter)` 76 76 ` {` `@@ -81,28 +81,23 @@ void Controller::CostFLow()` 81 81 ` ++ idx_ele;` 82 82 ` ` 83 83 ` if (BytheWay(*jter))` 84 `- addEdge(idx_order, idx_ele, 1, abs((*jter)->Get_pos() - (*iter)->from));` 84 `+ addEdge(idx_order, idx_ele, 1, abs((*jter)->Get_pos() - (*iter)->from)); // 对空闲的电梯和order连边` 85 85 ` ` 86 86 ` //addEdge(idx_order, idx_ele, 1, estimate(*jter, *iter));` 87 87 ` }` 88 88 ` }` 89 89 ` idx_ele = Emulator::orders.size();` 90 90 ` for (EleIter iter = ele_s; iter != ele_e; ++ iter)` 91 `- addEdge(++ idx_ele, T, 1, 0);` 91 `+ addEdge(++ idx_ele, T, 1, 0); // 每个电梯和T连边` 92 92 ` ` 93 `- int tt = 0;` 94 93 ` while (spfa())` 95 94 ` {` 96 `- ++ tt;` 97 95 ` /* Every path flow could only be one */` 98 96 ` for (int i = T; i != S; i = path[i])` 99 97 ` flow[path[i]][i] = 0, flow[i][path[i]] = 1;` 100 98 ` /* No need to calc the mincost */` 101 99 ` }` 102 `- //printf("%lu %d\n", Emulator::orders.size(), tt);` 103 `- //if (! tt) return;` 104 100 ` ` 105 `- //memset(toDel, 0, sizeof(toDel));` 106 101 ` idx_order = 0;` 107 102 ` for (OrderIter iter = order_s; iter != order_e; )` 108 103 ` {` `@@ -112,7 +107,7 @@ void Controller::CostFLow()` 112 107 ` for (EleIter jter = ele_s; jter != ele_e; ++ jter)` 113 108 ` {` 114 109 ` ++ idx_ele;` 115 `- if (flow[idx_ele][idx_order] == 1)` 110 `+ if (flow[idx_ele][idx_order] == 1) // 判断这个order是不是被接了，如果被接了，那么有一个电梯的流会被堵住。` 116 111 ` {` 117 112 ` (*jter)->PushInst(new Instruction((*iter)->from, 1));` 118 113 ` flag = 1;`