HDU 3001 Travelling(状压dp)
来源:互联网 发布:阿里云服务器怎样使用 编辑:程序博客网 时间:2024/05/24 03:44
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 11 2 1003 21 2 402 3 503 31 2 31 3 42 3 10
Sample Output
100907
思路: 3进制进行压缩,0 1 2 代表经过这个点的次数,每次利用一个点更新它可以到达的状态,具体实现在代码中
#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-8typedef __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 12int type[N]={0,1,3,9,27,81,243,729,2187,6561,19683,59049};int dp[59050][12]; //dp[i][j] i 状态下终点为j的情况int dig[59050][12]; //dig[i][j] i 状态下第j位走过的次数int dis[N][N]; // i~j的距离int n,m;void inint(){int i,j,t;fre(i,0,59050) { int t=i; fre(j,1,12) { dig[i][j]=t%3; t/=3; if(!t) break; } }}int main(){ int i,j; inint(); while(~sff(n,m)) { mem(dis,INF); mem(dp,INF); int s,e,len; while(m--) { sfff(s,e,len); if(dis[s][e]>len) dis[s][e]=dis[e][s]=len; } fre(i,1,n+1) dp[type[i]][i]=0; len=type[n+1]; int cur; int ans=INF; bool all_vis; fre(cur,0,len) { all_vis=true; //判断 cur 状态 是不是所有点都走过 fre(i,1,n+1) { if(dig[cur][i]==0) //根据i更新别的点,如果i没有到达,那么continue; { all_vis=false;continue; } if(dp[cur][i]==INF) continue; fre(j,1,n+1) { if(i==j) continue; if(dig[cur][j]>=2||dis[i][j]==INF) continue; //j已经被走2便了 int to=cur+type[j]; dp[to][j]=min(dp[to][j],dp[cur][i]+dis[i][j]); } } if(all_vis)fre(j,1,n+1) ans=min(ans,dp[cur][j]); } if(ans==INF) ans=-1; pf("%d\n",ans); }return 0;}
2 0
- HDU 3001Travelling (状压DP)
- HDU 3001 Travelling(状压DP)
- HDU 3001 Travelling(状压dp)
- HDU 3001 Travelling(状压dp)
- HDU 3001 Travelling(状压DP)
- HDU 3001 Travelling (状压dp三进制)
- HDU 3001 Travelling (状压DP)
- Travelling - HDU 3001 状压dp
- HDU 3001 Travelling 状压DP
- 状压DP hdu 3001 Travelling
- hdu 3001 Travelling 状压dp
- [HDU] 3001 Travelling [状压DP]
- hdu 3001 Travelling (TSP 三进制,状压dp)
- hdu 3001 Travelling 状压dp TSP变形
- 状压DP——Travelling ( HDU 3001 )
- hdu 3001 Travelling (TSP问题,状压dp)
- HDU 3001 Travelling(状态压缩DP)
- HDU 3001-Travelling(三进制状压DP)
- Access中批量替换数据库内容
- Android中的不同drawable文件夹所带来的思考
- POJ 2312 Battle City(BFS+优先队列)
- Please check that values for params "default.fs.name" and "hive.metastore.warehouse.dir" do not conf
- 第168章 大战!88真人娱乐城
- HDU 3001 Travelling(状压dp)
- 高通8x12平台 mipi屏 调试 .
- Protocol Buffers 语法指南
- hdu 1753 大明A+B
- zepto 研究
- 高通 8x12 添加 TP和按键 .
- Opengl开发环境配置
- [知识分享]浏览器地址栏快捷键的用例设计
- 关于hosts文件