Salesmen

Traveling salesmen of nhn. (the prestigious Korean internet company) report their current location to the company on a regular basis. They also have to report their new location to the company if they are moving to another location. The company keep each salesman's working path on a map of his working area and uses this path information for the planning of the next work of the salesman. The map of a salesman's working area is represented as a connected and undirected graph, where vertices represent the possible locations of the salesman an edges correspond to the possible movements between locations. Therefore the salesman's working path can be denoted by a sequence of vertices in the graph. Since each salesman reports his position regularly an he can stay at some place for a very long time, the same vertices of the graph can appear consecutively in his working path. Let a salesman's working path be correct if two consecutive vertices correspond either the same vertex or two adjacent vertices in the graph.

For example on the following graph representing the working area of a salesman,

\epsfbox{p4256.eps}

a reported working path [1 2 2 6 5 5 5 7 4] is a correct path. But a reported working path [1 2 2 7 5 5 5 7 4] is not a correct path since there is no edge in the graph between vertices 2 a 7. If we assume that the salesman reports his location every time when he has to report his location (but possibly incorrectly), then the correct path could be [1 2 2 4 5 5 5 7 4], [1 2 4 7 5 5 5 7 4], or [1 2 2 6 5 5 5 7 4].

The length of a working path is the number of vertices in the path. We define the distance between two paths A = a1a2...an and B =b1b2...bn of the same length n as

distAB) =  $\displaystyle \sum^{​{n}}_{​{i=1}}$ d (  a ib i)

where

d (  ab) =  $\displaystyle \left\{\vphantom{ \begin{array}{cc} 0 & \mbox{if } a=b \\  1 & \mbox{otherwise} \end{array} }\right.$ $\displaystyle \begin{array}{cc} 0 & \mbox{if } a=b \\  1 & \mbox{otherwise} \end{array}$

Given a graph representing the working area of a salesman and a working path (possible not a correct path), A , of a salesman, write a program to compute a correct working path, B , of the same length where the distance dist(AB) is minimized.


感觉学习动态规划遇到瓶颈了,每次做题都找不到最优子结构,总是被过去遇到的问题影响,没有创造性的思维。

d[i][q] = min { d[i-1][p] + 1 | (p, q) ∈E} 如果当前点与输入路径不吻合

d[i][q] = min { d[i-1][p] | (p, q) ∈E} 否则


int solve(int len, int n)

{
for(int i = 1; i <= n; i++)
{
if(i == path[0]) dp[0][i] = 0;
else dp[0][i] = 1;
}

for(int p = 1; p < len; p++)
{
for(int i = 0; i <= n; i++)
{
dp[p][i] = INF;

for(int k = 0; k <= n; k++)
{
if(graph[i][k])
{
if(i == path[p])
{
dp[p][i] = min(dp[p][i], dp[p-1][k]);
}
else
{
dp[p][i] = min(dp[p][i], dp[p-1][k] + 1);
}
}
}
}
}

int best = dp[len-1][1];
for(int k = 2; k <= n; k++)
{
if(dp[len-1][k] < best) best = dp[len-1][k];
}

return best;

}


我一直感觉这样的题目用动态规划做会很复杂,有很多不必要的状态被计算了,所以总是想着找一种更加高效的方法免去计算不必要的子问题。

这个方案中以前面的路径为子问题,其实当前的决策要依赖的就是前面的决策中可以到达当前所选择的点的所有子问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值