首页 技术 正文
技术 2022年11月15日
0 收藏 354 点赞 4,989 浏览 2414 个字

D – Hie with the Pie

Crawling in process… Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Status

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

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

题意:
一个人从起点出发去n个地方送货,最后回到起点,给出每两个点之间的距离计算最短路程。

代码:

 /*
dp[i][j]表示状态i时到达j点的时间,dp[i][j]=min(dp[i][j],dp[h][k]+d[k][j]),h表示的状态中没有j点
d[k][j]表示k点到j点的距离,由于是求最短要用一个flody算法算出最短距离
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n;
int d[][];
int dp[<<][];
int min(int x,int y)
{
return x<y?x:y;
}
int main()
{
while(scanf("%d",&n))
{
if(n==) break;
n+=;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
scanf("%d",&d[i][j]);
}
for(int k=;k<n;k++)
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
if(d[i][j]>d[i][k]+d[k][j])
d[i][j]=d[i][k]+d[k][j];
}
for(int i=;i<(<<n);i++)
{
for(int j=;j<n;j++)
{
if(i==(<<j))
dp[i][j]=d[][j]; //从0点可以直接到达的点
else if(i&(<<j)) //当i状态中包含j点时
{
dp[i][j]=; //初始化
for(int k=;k<n;k++)
{
if((j!=k)&&(i&(<<k))) //k与j非同一个点,i状态中包含k点
dp[i][j]=min(dp[i][j],dp[i^(<<j)][k]+d[k][j]); //不包含j点但有k点的状态
//加上d[k][j]
}
}
}
}
int ans=;
for(int i=;i<n;i++)
ans=min(ans,dp[(<<n)-][i]+d[i][]);
cout<<ans<<endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848