floyd,旅游问题每个点都要到,可重复,最后回来,dp
Hie with the Pie
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4013   Accepted: 2132

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 jwithout 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点出发,送外卖到n个地方,给出每个点之间的距离,帮他选择一条最短路回到原点。注意他走的节点可以重复。
分析:
先用floyd求每俩个点,然后进行dp,dp[i][j],表示最后到达i城市j状态,1表示以到达城市,0未到达。
是用状态来更新城市,比如011,最后到达第1个城市可以从010改变而来,则010就必须要有值。
具体见代码解释。先不考虑回来,只算到状态全1的情况,看哪个城市最后到达。
//dp[i][j],最后到达城市i,状态j,最小的路径。
#include<iostream>
#include<cstring>
#include<cstdio>
#define inf 1<<27
using namespace std;
int dis[][],dp[][];
int n;
void floyd()//求俩点的最短距离。
{
int i,j,k;
for(k=; k<=n; k++)
for(i=; i<=n; i++)
for(j=; j<=n; j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
int main()
{
int i,j,s,k;
while(~scanf("%d",&n)&&n!=)
{
for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d",&dis[i][j]);
floyd();
for(s=; s<<<n; s++)//枚举所有的状态。
{
for(i=;i<=n; i++)//除去0城市。
{
if(s&<<(i-))//是否到达i个城市,s状态的i位是否为1.
{
if(s==<<(i-))//状态只能从0到达i个城市。
dp[i][s]=dis[][i];//初始化。dp的边界。
else//状态除了0外还可以从别的城市出发,但这个城市一定是状态已经为1的城市,到达i城市。
{
dp[i][s]=inf;
for(k=; k<=n; k++)
if((k!=i)&&(s&<<(k-)))//k一定是s状态里已经到达的为1.
dp[i][s]=min(dp[i][s],dp[k][s^(<<(i-))]+dis[k][i]);
//s^(1<<(i-1)),s的i位一定是1,但要变为0,处理。 }
}
}
}
int ans=inf;
for(i=;i<=n;i++)
{ ans=min(ans,dp[i][(<<n)-]+dis[i][]);
//先不考虑回来,只算到状态全1的情况,看哪个城市最后到达。 }
printf("%d\n",ans);
}
return ;
}
												

最新文章

  1. MySql查询数据库的大小
  2. VBS基础篇 - 运算符
  3. 【Entity Framework】 Entity Framework资料汇总
  4. c语言面试题之sizeof
  5. [iOS] Create TableView &amp; customize UITableViewCell
  6. RelativeLayout的一些感想
  7. position:sticky 定位 position:fixed
  8. 页面中iframe中嵌入一个跨域的页面,让这个页面按照嵌入的页面宽高大小显示的方式;iframe嵌套的页面不可以编辑的问题解决方案
  9. 访问System x3650 IMM2的几种方式
  10. python的扩展包requests的高级用法
  11. HUAWEI USG6000系列 &amp; NGFW Module V100R001 典型配置案例
  12. Microsoft.AspNet.Identity: UserID用整型数据表示, 而不是GUID
  13. eclipse web module版本问题:Cannot change version of project facet Dynamic Web Module to 2.5.
  14. PHP中使用OpenSSL生成RSA公钥私钥及进行加密解密示例(非对称加密)
  15. 我的Visual Studio 2013常用快捷键
  16. Eletron 打开文件夹,截图
  17. Apache Commons IO之FileUtils的常用方法
  18. gitolite
  19. Debian安装Chrome
  20. JNI简单HelloWorld

热门文章

  1. 包含中文的字符串中截取前N个字符
  2. 李洪强漫谈iOS开发[C语言-040]-switch case
  3. 写出优秀论文How To Write A Great Essay About Anything
  4. bootstrap 3.x笔记
  5. web前端性能测试小点
  6. Ubuntu 12.04搭建MTK 6577 安卓开发环境
  7. [51NOD1105]第k大的数(二分答案)
  8. gulp 使用mailgun服务器发送邮件
  9. JUnit4概述
  10. How can I add a new user as sudoer using the command line?