poj3311 Hie with the Pie

Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3025 Accepted: 1543

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

Source

求经过每一个点最终还要回到出发点经过的最短距离。说实话这个题的题目我是看了好久的,跟英语有很大关系,一开始怎么都不知道题目说的什么意思,本来看题目的输入数据以为是二维的图,最后百度了好多解题报告才知道这个图是线性的,坑啊。。。
这个题要先预处理一下,求出ij之间的距离,之后的看代码吧
Memory: 320 KB Time: 0 MS Language: C Result:  Accepted
#include<stdio.h>
#include<string.h>
//【状态表示】dp[state][i]表示到达i点状态为state的最短距离

//【状态转移方程】dp[state][i] =min{dp[state][i],dp[state'][j]+dis[j][i]} dis[j][i]为j到i的最短距离

//【DP边界条件】dp[state][i] =dis[0][i]  state是只经过i的状态
int map[13][13],dp[3000][13];

int min(int x,int y)
{
	return x>y?y:x;
}

int main()
{
	int i,j,k,x,n;
	while(scanf("%d",&n)!=EOF&&n)
	{
		for(i=0;i<=n;i++)
			for(j=0;j<=n;j++)
				scanf("%d",&map[i][j]);
		for(i=0;i<=n;i++)
			for(j=0;j<=n;j++)
				for(k=0;k<=n;k++)
					if(i!=j&&j!=k&&i!=k)
						map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
		memset(dp,-1,sizeof(dp));//注意不能用大数据来初始化,这个别搞错了
		dp[1][0]=0;
		n++;
		for(i=1;i<(1<<n);i++)//从下往上递推。
		{
			for(j=0;j<n;j++)
			{
				if(dp[i][j]==-1)//i状态下j还没走过
					continue;
				for(k=1;k<n;k++)//k从0或1开始.测试结果一样
				{
					if((1<<k)&i)//选择了K点
						continue;
					x=((1<<k)|i);//包含了k的一个状态,如(i)11010和(1<<k)100
					if(dp[x][k]==-1)
						dp[x][k]=dp[i][j]+map[j][k];//i状态到j点最短距离+j到k最短距离
					else
						dp[x][k]=min(dp[x][k],dp[i][j]+map[j][k]);//此时i是不包含k的,所以要加map[j][k]
					//?对于包含k的x状态,其值可能是已经从前往后递推出来的dp[x][k],也有可能是后来又推得的i状态到j点最短距离+j到k最短距离
					//printf("k%d-%d\n",x,dp[x][k]);
				}
			}
			//printf("i%d ",i);
		}
		x=10000000;
		for(i=1;i<n;i++)
		{
			if(dp[(1<<n)-1][i]>0)//最终所有点都经过的状态到每个点i的距离+map[i][0](此处0是起始点),因为题目要求还要回到原点
				x=min(x,dp[(1<<n)-1][i]+map[i][0]);
		}
		printf("%d\n",x);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值