设为首页 加入收藏

TOP

hdu 3001 Travelling (bfs+状态压缩)
2015-07-20 17:33:54 来源: 作者: 【 】 浏览:1
Tags:hdu 3001 Travelling bfs 状态 压缩

Travelling

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


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 

网上说是DP+状压,结果用bfs+状压水过了。。

开始时把每一个点都入队,模拟3进制处理每个状态,最后+优化。

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          using namespace std; #define N 12 #define LL long long const int inf=0x3fffffff; int g[N][N]; int n,m,ans; int mark[N][60000]; struct node { int x,t,s,cnt; //位置、时间、状态、个数 friend bool operator<(node a,node b) { return a.t>b.t; } }; int gettmp(int x,int k) //得到X在3进制下的第K位是多少 { int t; while(x) { t=x%3; k--; if(k==0) break; x/=3; } return k?0:t; } void inti() //初始化数组 { int i,j; for(i=1;i<=n;i++) { for(j=0;j<(int)pow(3,n);j++) mark[i][j]=inf; } } void bfs() { int i; priority_queue
         
          q; node cur,next; for(i=1;i<=n;i++) { cur.x=i; cur.s=pow(3,(i-1)); cur.t=0; cur.cnt=1; q.push(cur); mark[i][0]=0; } while(!q.empty()) { cur=q.top(); q.pop(); for(i=1;i<=n;i++) { if(g[cur.x][i]==inf) continue; next.cnt=cur.cnt; next.s=cur.s; next.t=cur.t+g[cur.x][i]; if(ans
          
           =2) continue; next.s+=pow(3,(i-1)); if(t==0) { next.cnt++; if(next.cnt==n) { ans=min(ans,next.t); continue; } } if(next.t
           
            

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇动态内存管理---new&delete 下一篇BZOJ 3110 ZJOI2013 K大数查询 树..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)