hdu3001(集合dp求哈密顿回路)

Travelling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2460    Accepted Submission(s): 707


Problem Description
After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
 

Input
There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.
 

Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
 

Sample Input
      
      
2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3 10
 

Sample Output
      
      
100 90 7
 


题意:给定n个顶点,m条边(带边权),每个点最多访问两次,求哈密顿路径。

思路:也是集合dp的思路,各种限制比hdu3538少,不同的是每个位置需要表示3个状态,0代表没有访问过,1代表访问了一次,2代表访问了两次。这样用二进制的状态压缩就不行了,需要三进制,虽然方法一样。。不过第一次搞二进制之外的压缩需要慢慢来。。(其实我好慢)。dp[s][i]同样表示点集合s里面以i为终点的最短路径长度。详细看代码注释。hdu3538题解:http://blog.csdn.net/xishisugan/article/details/8747884

#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 82969;
//这是提前计算出来的3^0~3^11
int t3[11] = {1,3,9,27,81,243,729,2187,6561,19683,59049,177147};
int bit[N][11];//bit[i][j]表示三进制数i的第j位是几
int dp[N][11];
int dis[11][11],n;//邻接矩阵
void init()
{
    for(int i = 0;i < t3[10];i ++)//遍历每一个状态
      {
          int tmp = i;
          for(int j = 0;j < 10;j ++)//n最大是10
          {
              bit[i][j] = tmp%3;//取个位数字,
              tmp/=3;
          }
      }
}
bool ok(int s)//函数用于判断是否所有有效位都不为零,这样代表所以点都走过了
{
    for(int i = 0;i < n;i ++)//n是几就几个位合法,别写10,多余位是不合法的
        if(bit[s][i] == 0)return false;
    return true;
}
int main(){
   
      int m;
      init();//预处理计算出由于3进制到二进制而不能直接用位运算得到的结果
      while(~scanf("%d%d",&n,&m))
      {
          memset(dis,INF,sizeof(dis));
          memset(dp,INF,sizeof(dp));
          while(m--)
          {
              int a,b,c;
              scanf("%d%d%d",&a,&b,&c);
              a--,b--;//题目要求是1~n要压缩必须是0~n所以需要减一
              dis[a][b] = dis[b][a] = min(c,dis[a][b]);//避免重边取最小
          }

	//因为可以随意选择出发点,所以对于每种只含一位1并且以自己为终点的状态都合法
           for(int i=0; i<n; i++)dp[t3[i]][i]=0; 

          //dp
          for(int s = 0;s < t3[n];s ++)//枚举集合
          {
              for(int i = 0;i < n;i ++)//枚举集合终点
              {
		//出发点不在集合里面或者状态不合法就不能拓展
                  if(!bit[s][i] || dp[s][i] == INF)continue;

                  for(int j = 0;j < n;j++)//枚举要到达的点
                  {
			//到达的点不能是自己,走了两次也不行
                      if(j == i || bit[s][j] == 2)continue;
                      if(dis[i][j] == INF)continue;//没有路当然不能走

                      int S = t3[j] + s;//状态里加入j,S为新状态
                      dp[S][j] = min(dp[S][j],dp[s][i]+ dis[i][j]);
                  }
              }
          }
          int ans = INF;//找答案
          for(int s = 0;s < t3[n];s ++)
          {
              if(ok(s))//判断状态各位是不是都被遍历过
              for(int i = 0;i < n;i ++)
                  ans = min(ans,dp[s][i]);
          }
          if(ans == INF)puts("-1");
          else printf("%d\n",ans);
      }
   return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值