Hie with the Pie_poj3311

99 篇文章 0 订阅
56 篇文章 0 订阅

<span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Description</span>

The Pizazz Pizzeria prides itself indelivering pizzas to its customers as fast as possible. Unfortunately, due tocutbacks, they can afford to hire only one driver to do the deliveries. He willwait for 1 or more (up to 10) orders to be processed before he starts anydeliveries. Needless to say, he would like to take the shortest route indelivering these goodies and returning to the pizzeria, even if it meanspassing the same location(s) or the pizzeria more than once on the way. He hascommissioned you to write a program to help him.

有不超过十个客户,已知地址和走完两两相邻地点路程的时间,求一条最短路经过所有点并返回起点(0,0)

Input

Input will consist of multiple test cases.The first line will contain a single integer n indicating the number of ordersto deliver, where 1 ≤ n ≤ 10. Afterthis will be n + 1 lines each containing n + 1 integers indicating the times totravel between the pizzeria (numbered 0) and the n locations (numbers 1 to n).The jth value on the ith line indicates the time to go directly from location ito location j without visiting any other locations along the way. Note thatthere may be quicker ways to go from i to jvia other locations, due todifferent speed limits, traffic lights, etc. Also, the time values may not besymmetric, i.e., the time to go directly from location i to j may not be thesame as the time to go directly from location j to i. An input value of n = 0will terminate input.

以邻接表的形式输入n+1行,n+1列,第I行j列表示i-1,j-1行驶过两点的时间。输入n为0时表示结束。

Output

For each test case, you should output asingle number indicating the minimum time to deliver all of the pizzas andreturn to the pizzeria.

最短时间

Sample Input

3

0 1 10 10

1 0 1 2

10 1 0 10

10 2 10 0

0

Sample Output

8

思路:

  由于给出的是连接矩阵求最短路,想到用floyd求每两对点之间的最短距离。又看到最优,可以确定是动态规划。F[I,j]=min(f[Ixor (1 shl (k-1))),k]+dist[k,j],f[I,j]), i用二进制表示当前状态。注意要加上回来的路程求最小值。

代码:

var n,i,j,k,l:longint;
d,f:array[0..1200,0..1200]of longint;
x,y,z,t,ans:Longint;
function min(x,y:longint):longint;
begin
  if x<y then
  min:=x
  else
  min:=y;
end;
begin
  readln(n);
  if n<>0 then
  repeat
    fillchar(f,sizeof(f),$7f div 3);
    f[0,1]:=0;
    for i:=0 to n do
    for j:=0 to n do read(d[i,j]);
    for k:=0 to n do
    for i:=0 to n do
    for j:=0 to n do
    if (i<>j)and(k<>j)and(i<>k)and(d[i,k]+d[k,j]<d[i,j]) then d[i,j]:=d[i,k]+d[k,j];
    for i:=0 to 1shl n-1 do
    for j:=1 to n do
    if i=1 shl(j-1) then
    f[i,j]:=d[0,j]
    else
    begin
      l:=i xor(1shl(j-1));
      for k:=1 to n do
      if (l and(1shl(k-1)))<>0 then
      f[i,j]:=min(f[l,k]+d[k,j],f[i,j]);
    end;
    ans:=maxlongint;
    for i:=1 to n do
    if f[1shl n-1,i]+d[i,0]<ans then
    ans:=f[1shl n-1,i]+d[i,0];
    writeln(ans);
    readln(n);
  until n=0;
end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值