HDU3001 Travelling(DP)
来源:互联网 发布:阿里云服务器无法连接 编辑:程序博客网 时间:2024/05/29 03:18
Travelling
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6388 Accepted Submission(s): 2069
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
Source
2009 Multi-University Training Contest 11 - Host by HRBEU
Recommend
gaojie
/* * HDU3001状态压缩DP */#include <iostream>#include <string.h>#include <algorithm>using namespace std;const int MAXN=15;const int INF=0x3f3f3f3f;int g[MAXN][MAXN];int dp[60000][MAXN];int p[12];int n;int a[12];int ArrayToInt(int a[]){ int ret=0; for(int i=n-1;i>=0;i--) { ret*=3; ret+=a[i]; } return ret;}void intToArray(int x){ for(int i=0;i<n;i++) { a[i]=x%3; x/=3; }}int main(){ p[0]=1; for(int i=1;i<=11;i++)p[i]=3*p[i-1]; int u,v,w; int m; while(scanf("%d%d",&n,&m)==2) { memset(g,-1,sizeof(g)); while(m--) { scanf("%d%d%d",&u,&v,&w); u--; v--; if(g[u][v]==-1)g[u][v]=g[v][u]=w; else g[u][v]=g[v][u]=min(w,g[u][v]); } if(n==1) { printf("0\n"); continue; } memset(dp,-1,sizeof(dp)); int tot=0; for(int i=0;i<n;i++)tot+=p[i]; for(int i=0;i<n;i++)dp[p[i]][i]=0; int ans=INF; for(int i=0;i<p[n];i++) for(int j=0;j<n;j++) { intToArray(i); if(a[j]==0)continue; if(dp[i][j]<0)continue; bool full=true; for(int t=0;t<n;t++) if(a[t]==0) full=false; if(full)ans=min(ans,dp[i][j]); for(int k=0;k<n;k++) if(j!=k && g[j][k]!=-1 && a[k]<2) { a[k]++; int s=ArrayToInt(a); if(dp[s][k]==-1)dp[s][k]=dp[i][j]+g[j][k]; else dp[s][k]=min(dp[s][k],dp[i][j]+g[j][k]); a[k]--; } } if(ans==INF)printf("-1\n"); else printf("%d\n",ans); } return 0;}
0 0
- HDU3001 Travelling(DP)
- hdu3001 Travelling(状态压缩dp,三进制)
- 【DP】 hdu3001 Travelling
- HDU3001 Travelling 状压DP
- HDU3001——Travelling(状态压缩DP)
- (状态压缩) Travelling (HDU3001)
- hdu3001 Travelling 三进制状态压缩dp
- hdu3001 travelling
- HDU3001 Travelling
- hdu3001 Travelling
- HDU3001 Travelling
- HDU3001 Travelling
- hdu3001 Travelling(TSP+三进制)
- HDU3001-Travelling-状态压缩
- (复习)(转)03进制状态压缩DP——HDU3001 Travelling 旅行商问题
- hdu3001(状态压缩dp)
- hdu3001 Travelling 三进制状态压缩
- 3进制状态压缩DP——HDU3001 Travelling 旅行商问题
- 数字信封
- 基于jquery,bootstrap数据验证插件bootstrapValidator 教程
- linphone之elapsed>=lc->sip_conf.delayed_timeout分析
- python 版本控制及django,git的使用
- mysql 主从 配置
- HDU3001 Travelling(DP)
- JQ+MYSQL实现省市区联动JQ部分
- [LeetCode] 7. Reverse Integer
- Reverse Vowels of a String
- VF01生成billing凭证:BAPI_BILLINGDOC_CREATEMULTIPLE
- Xcode 8 的新功能一览
- Nginx简单部署实现反向代理的功能,附带资源包
- Java 遍历Map的方法
- CodeChef 2016年7月问题