Hie with the Pie
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4671   Accepted: 2471

Description

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

Input

Input will consist of multiple test cases. The first line will contain a single integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating
the times to travel 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 i to location j without visiting
any other locations along the way. Note that there may be quicker ways to go from i to j via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from
location i to j may not be the same as the time to go directly from location j to i. An input value of n = 0 will terminate input.

Output

For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return 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

题意

从0出发送汉堡,送完每一个点后回到0点

矩阵 表示各个点间距离。

思路

由于能够各个点多次经过。所以先求下floyd 来更新点点之间最短距离。

把每一步。到各个点的状态和最后一步所在的位置还有所花的距离保存下来。

把最后到的各个地方,再加个回零点的距离。求个最小值。

dp[15][15]   一维表示已经走的步数,二维表示当前最后一步到的点。  map的x表示状态,y表示已经花费的时间。

#include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
map<int,int>::iterator it;
map<int,int> dp[15][15];//ceng 结尾
int mp[15][15];
void floyd(int n)
{
int i,j,k;
for(k=0;k<=n;k++)
{
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
}
}
}
int main()
{
int n,i,j,k,mn,x,y;
while(scanf("%d",&n),n)
{
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
scanf("%d",&mp[i][j]);
}
}
floyd(n);
/*for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
printf("%d ",mp[i][j]);
}
puts("");
}*/ //clear
dp[0][0][1]=0;
for(i=1;i<=n;i++)//第几处了
{
for(k=0;k<=n;k++)//终点
{
dp[i][k].clear();
for(j=0;j<=n;j++)//起点
{
for(it=dp[i-1][j].begin();it!=dp[i-1][j].end();it++)//上一层的各个状态
{
x=it->first;
y=it->second;
if((x&(1<<k))==0)//还没走过
{
if(dp[i][k].count(x|(1<<k))==0)
dp[i][k][x|(1<<k)]=y+mp[j][k];
else
dp[i][k][x|(1<<k)]=min(dp[i][k][x|(1<<k)],y+mp[j][k]);
}
}
}
}
} i=n;//最后一层
mn=999999999;
for(j=1;j<=n;j++)
{
for(it=dp[i][j].begin();it!=dp[i][j].end();it++)//上一层的各个状态
{
mn=min(mn,it->second+mp[j][0]);
}
}
printf("%d\n",mn); }
return 0;
}
/*
3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0 3
0 5 6 7
5 0 8 9
6 8 0 10
7 9 10 0
*/

最新文章

  1. ExecuteReader在执行有输出参数的存储过程时拿不到输出参数
  2. C#汉字转拼音帮助类
  3. 首师大附中科创教育平台 我的刷题记录 3120 LJX的校园:入学典礼
  4. java与mysql连接
  5. Dojo注意
  6. phpStudy启动失败时的解决方法
  7. ORA-01000:超出打开游标的最大数(C#)
  8. 夺命雷公狗ThinkPHP项目之----企业网站20之网站前台头尾分离
  9. Word Ladder
  10. 对于利用ica进行fmri激活区识别的理解
  11. my97datepicker开始日期小于结束日期格式化时间精确届时分秒
  12. 【easy】100. Same Tree
  13. 第三周Access的总结
  14. Internet
  15. Axiom3D:Ogre公告板集与合并批次
  16. Spring框架之log日志的使用
  17. EF简易教程,从建表到表间关系
  18. java 和 javascript CryptoJS 进行HmacSHA1加密
  19. matlab随笔
  20. 以太坊入门-solidity环境搭建

热门文章

  1. Week One
  2. [转] Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架
  3. char *p 与char p[]
  4. 【转】2012年7月9 – 知名网页游戏公司 PHP高级工程师 最新面试题
  5. mysql localhost登录和tcp/ip登录 strace
  6. promise和Rxjs的一点区别
  7. Visual Studio 2013在线文档地址备案
  8. mysql知识点(三)
  9. Oracle sql&quot;NOT IN&quot;语句优化,查询A表有、B表没有的数据
  10. jQuery EasyUI API 中文文档 - Panel面板