不好意思,我略过了一些步骤。 把每个vertice分拆成两个vertice, create two edges between them, one with INF capacity and 0 cost, one with capacity of 1 and cost of -money. Edges between the original vertices stays the same, with 0 cost and INF capacity( or capacity of 2, does not matter). This is the standard way to move cost/capacity to edges.
【在 h****t 的大作中提到】 : The cost of minimum-cost flow problem is on the edges, not on the vertices.
h*t
19 楼
多谢,受教了。这个面试题未免太难了点。
U*A
20 楼
有没有人给个算法。
f*s
21 楼
If there is one robot, it can be at each of m*n locations, with DP, calculation needs to be done once only for each location so the time complexity is O(m*n). If there are two robots, they can be at each of (m*n)*(m*n) locations, again with DP, calculation needs to be done once only for each location so the time complexity is O(m*n*m*n).
U*A
22 楼
给个具体算法?
again
【在 f*********s 的大作中提到】 : If there is one robot, it can be at each of m*n locations, with DP, : calculation needs to be done once only for each location so the time : complexity is O(m*n). : If there are two robots, they can be at each of (m*n)*(m*n) locations, again : with DP, calculation needs to be done once only for each location so the : time complexity is O(m*n*m*n).
j*g
23 楼
int twoRobs(vector > &MP){ int M = MP.size(); int N = MP[0].size(); int P_LEN = M + N -1; vector > dp1 = vector >(N, vector(N, 0)); vector > dp2 = vector >(N, vector(N, 0)); dp1[0][0] = MP[0][0]; dp2[0][0] = MP[0][0]; for (int p = 2; p <= P_LEN; p++){//p is the length of the path for (int x1 = 0; x1 < N; x1++){ for (int x2 = x1; x2 < N; x2++){ int y1 = p - 1 - x1; int y2 = p - 1 - x2; if(y1<0 || y2<0 || y1>=M || y2>=M) continue; int best = 0; int delta = MP[y1][x1]; if (x1 != x2) delta += MP[y2][x2]; if(x1>0 && x2>0)//from left left best = max(best, dp1[x1-1][x2-1] + delta); if(x1>0 && y2>0)//from left up best = max(best, dp1[x1-1][x2] + delta); if(y1>0 && x2>0)//from up left best = max(best, dp1[x1][x2-1] + delta); if(y1>0 && y2>0)//from up up best = max(best, dp1[x1][x2] + delta); dp2[x1][x2] = best; } } dp1=dp2; } return dp1[N-1][N-1]; }