POJ 3311 Hie with the Pie(状压dp or dfs)

Language:
Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5057 Accepted: 2695

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 ito 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



题意: 由0出发把所有的点走一遍后回到0,求最短的距离


思路:二进制 1表示走过,0没走,然后模拟当前的状态可以由哪些状态到达


思路2:dfs



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 11

int dis[N][N],dp[1<<N][N];
int n;

void floyd()
{
	int i,j,k;
	fre(k,0,n+1)
	 fre(i,0,n+1)
	   fre(j,0,n+1)
	     dis[i][j]=min(dis[i][k]+dis[k][j],dis[i][j]) ;

}


int main()
{
	int i,j;
    while(sf(n),n)
	{
		fre(i,0,n+1)
		 fre(j,0,n+1)
		  sf(dis[i][j]);

		 floyd();   //这一步很重要
		 
		 mem(dp,INF);

		 int cur;
		 int len=1<<n;
		 fre(cur,0,len)
		   fre(i,1,n+1)
		    {
		    	 if(!(cur&(1<<(i-1)))) continue;

		    	 if(cur==(1<<(i-1)))      //初始条件,由0开始走  走的第一个点
				 {
				     dp[cur][i]=dis[0][i];
				     continue;
  				 }

	             fre(j,1,n+1)
	               if(cur&(1<<(j-1))&&i!=j)
					 dp[cur][i]=min(dp[cur][i],dp[cur^(1<<(i-1))][j]+dis[j][i]);

					 //枚举到过j,但是没有到过i的点走到i的过程

	      }

       int ans=dp[len-1][1]+dis[1][0];

       fre(j,2,n+1)
         ans=min(ans,dp[len-1][j]+dis[j][0]);

		pf("%d\n",ans);
	}

return 0;
}

/*

 dfs 版本
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 15

int a[N],dis[N][N],vis[N];
int ans,n;

void solve()
{
	int i,j;
	int temp=0;
	a[n+1]=0;

    fre(i,1,n+2)
	  temp+=dis[a[i-1]][a[i]];

	if(temp<ans) ans=temp;
}

void dfs(int pos)
{
	int i,j;
	if(pos==n+1)
	{
		 solve();
		 return ;
	}

	fre(i,1,n+1)

	  if(!vis[i])
	{
	    a[pos]=i;
	    vis[i]=1;
	    dfs(pos+1);
	    vis[i]=0;
	}
}

void floyd()
{
	int i,j,k;
	fre(k,0,n+1)
	  fre(i,0,n+1)
        fre(j,0,n+1)
         dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}

int main()
{
    int i,j;
    while(sf(n),n)
	{
		fre(i,0,n+1)
		 fre(j,0,n+1)
		   sf(dis[i][j]);


        floyd() ;

		a[0]=0;
		ans=INF;
		mem(vis,0);
		dfs(1);

		pf("%d\n",ans);
	}

    return 0;
}


*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值