Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 8fe345abd14ffaf7b61dd5f7629bec728509b081 1 parent dca6b04
Kaikai Zhu authored
Showing with 7 additions and 12 deletions.
  1. +7 −12 Sources/costflow.cpp
View
19 Sources/costflow.cpp
@@ -23,13 +23,13 @@ int inq[MAXN], Q[MAXN], dist[MAXN];
* 构图做最小费用最大流,以保证在当前情况下满足最多数量的order并且总代价最小
*/
-void addEdge(int a, int b, int f, int c)
+void addEdge(int a, int b, int f, int c) //加边函数,构图时用。a到b添加流量为f的,费用是c的边。
{
flow[a][b] = f, flow[b][a] = 0;
cost[a][b] = c, cost[b][a] = -c;
}
-int BytheWay(Elevator *ele)
+int BytheWay(Elevator *ele) // 判断电梯是不是空闲。
{
if (ele->Get_nPassenger() == Elevator::Get_capacity())
return 0;
@@ -38,7 +38,7 @@ int BytheWay(Elevator *ele)
else return 0;
}
-int spfa()
+int spfa() // 费用流中求最小费用权路径。用spfa实现以加速。
{
memset(dist, -1, sizeof(dist));
memset(inq, 0, sizeof(inq));
@@ -70,7 +70,7 @@ void Controller::CostFLow()
int idx_order, idx_ele;
idx_order = 0;
for (OrderIter iter = order_s; iter != order_e; ++ iter)
- addEdge(S, ++ idx_order, 1, 0);
+ addEdge(S, ++ idx_order, 1, 0); // 每个order和S连边
idx_order = 0;
for (OrderIter iter = order_s; iter != order_e; ++ iter)
{
@@ -81,28 +81,23 @@ void Controller::CostFLow()
++ idx_ele;
if (BytheWay(*jter))
- addEdge(idx_order, idx_ele, 1, abs((*jter)->Get_pos() - (*iter)->from));
+ addEdge(idx_order, idx_ele, 1, abs((*jter)->Get_pos() - (*iter)->from)); // 对空闲的电梯和order连边
//addEdge(idx_order, idx_ele, 1, estimate(*jter, *iter));
}
}
idx_ele = Emulator::orders.size();
for (EleIter iter = ele_s; iter != ele_e; ++ iter)
- addEdge(++ idx_ele, T, 1, 0);
+ addEdge(++ idx_ele, T, 1, 0); // 每个电梯和T连边
- int tt = 0;
while (spfa())
{
- ++ tt;
/* Every path flow could only be one */
for (int i = T; i != S; i = path[i])
flow[path[i]][i] = 0, flow[i][path[i]] = 1;
/* No need to calc the mincost */
}
- //printf("%lu %d\n", Emulator::orders.size(), tt);
- //if (! tt) return;
- //memset(toDel, 0, sizeof(toDel));
idx_order = 0;
for (OrderIter iter = order_s; iter != order_e; )
{
@@ -112,7 +107,7 @@ void Controller::CostFLow()
for (EleIter jter = ele_s; jter != ele_e; ++ jter)
{
++ idx_ele;
- if (flow[idx_ele][idx_order] == 1)
+ if (flow[idx_ele][idx_order] == 1) // 判断这个order是不是被接了,如果被接了,那么有一个电梯的流会被堵住。
{
(*jter)->PushInst(new Instruction((*iter)->from, 1));
flag = 1;
Please sign in to comment.
Something went wrong with that request. Please try again.