码迷,mamicode.com
首页 > 其他好文 > 详细

poj3311 Hie with the Pie

时间:2015-06-22 15:07:09      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:状压dp

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

这是一道类似TSP的题,可以用状态压缩做,刚开始一直看不懂,看好久看懂了。。

思路是这样:先用floy记录任意两点之前的最小距离,0~n,然后用0,1表示所有的状态,dp[i][j]表示从0开始经过集合j中的所有点,且当前点是i的最小步数。然后再用一个循环来求出ans=dp[i][(1<<n)-1]+dis[i][0]的最小值。方法和floyd有点像。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define inf 0x7fffff
int dis[15][15],n,dp[15][2500];
void floyd()
{
	int i,j,k;
	for(k=0;k<=n;k++){
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++){
				if(i==j)dis[i][j]=0;
				else{ 
					if(dis[i][j]>dis[i][k]+dis[k][j]){
						dis[i][j]=dis[i][k]+dis[k][j];
					}
				}
			}
		}
	} 
}

int main()
{
	int m,i,j,s,ans;
	while(scanf("%d",&n)!=EOF && n!=0)
	{
		memset(dis,0,sizeof(dis));
		memset(dp,0,sizeof(dp));
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++){
				scanf("%d",&dis[i][j]);  //这里要注意,dis[i][j]和dis[j][i]不一定相同,这点让我wa了好几次。。 
			}
		}
		floyd();
		for(s=1;s<(1<<n);s++){
			for(i=1;i<=n;i++){
				if(s&( 1<<(i-1) )){
					if(s==(1<<(i-1)) )dp[i][s]=dis[0][i];
					else{
						dp[i][s]=inf;
						for(j=1;j<=n;j++){
							//if( ( (s^(1<<(i-1)))&j )  &&  (j!=i)){ 这里不能这么这么写,因为i的二进制可能有多个1,如果都异或完可能会把j覆盖 
							if(s&(1<<(j-1)) && j!=i){
								dp[i][s]=min(dp[i][s],dp[j][s^(1<<(i-1))]+dis[j][i]);
							}
						}
					}
				}
			}
		}
		ans=inf;
		for(i=1;i<=n;i++){
			if(ans>dp[i][(1<<n)-1]+dis[i][0]) //这里不能写成dis[0][i],因为两者可能不相同 
			ans=dp[i][(1<<n)-1]+dis[i][0];
		}
		printf("%d\n",ans);
	}
	return 0;
}



poj3311 Hie with the Pie

标签:状压dp

原文地址:http://blog.csdn.net/kirito_acmer/article/details/46592605

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!