状压DP HDU3538 A sample Hamilton path
来源:互联网 发布:linux shell 打印变量 编辑:程序博客网 时间:2024/06/10 10:08
A sample Hamilton path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 527 Accepted Submission(s): 213
Problem Description
Give you a Graph,you have to start at the city with ID zero.
Input
The first line is n(1<=n<=21) m(0<=m<=3)
The next n line show you the graph, each line has n integers.
The jth integers means the length to city j.if the number is -1 means there is no way. If i==j the number must be -1.You can assume that the length will not larger than 10000
Next m lines,each line has two integers a,b (0<=a,b<n) means the path must visit city a first.
The input end with EOF.
The next n line show you the graph, each line has n integers.
The jth integers means the length to city j.if the number is -1 means there is no way. If i==j the number must be -1.You can assume that the length will not larger than 10000
Next m lines,each line has two integers a,b (0<=a,b<n) means the path must visit city a first.
The input end with EOF.
Output
For each test case,output the shorest length of the hamilton path.
If you could not find a path, output -1
If you could not find a path, output -1
Sample Input
3 0-1 2 4-1 -1 21 3 -14 3-1 2 -1 12 -1 2 14 3 -1 13 2 3 -11 30 12 3
Sample Output
45
Hint
I think that all of you know that a!=b and b!=0 =。=
Source
2010 ACM-ICPC Multi-University Training Contest(11)——Host by BUPT
Recommend
zhouzeyong
莫名其妙就状压DP了,说实在我现在还不明白这是个什么玩意儿......
不过尹神说了是典型,那就写一写吧......
这就是个Floyd么......
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 int n,m; 8 int dis[25][25]; 9 int f[2500000];10 int dp[2500000][25];//dp[i][j]已访问的点集i 到达点i的最短hamilton 11 const int MAX=3000000;12 int ans;13 14 int main(){15 int x=0,y=0;16 while(scanf("%d%d",&n,&m)!=EOF){17 memset(f,0,sizeof(f));18 memset(dis,0,sizeof(dis));19 memset(dp,0,sizeof(dp)); 20 for(int i=0;i<n;i++)21 for(int j=0;j<n;j++) scanf("%d",&dis[i][j]);22 for(int i=1;i<=m;i++){23 scanf("%d%d",&x,&y);24 f[y]|=(1<<x);//观察了样例才发现 原来y可以多次出现 25 }26 for(int i=0;i<(1<<n);i++)27 for(int j=0;j<n;j++) dp[i][j]=MAX;28 dp[1][0]=0;29 for(int k=0;k<(1<<n);k++)30 for(int i=0;i<n;i++)31 if(dp[k][i]!=MAX)32 for(int j=0;j<n;j++){33 if((dis[i][j]==-1)||(!(k&(1<<i)))||(k&(1<<j))||f[j]!=(k&f[j])) continue;//判断是否遍历过 34 dp[k|(1<<j)][j]=min(dp[k|(1<<j)][j],dp[k][i]+dis[i][j]);//f[k][i]->f[k+{j}][j] + dis(i, j) 35 }36 ans=MAX;37 for(int i=0;i<n;i++) ans=min(ans,dp[(1<<n)-1][i]);38 if(ans==MAX) printf("-1\n");39 else printf("%d\n",ans);40 }41 return 0;42 }
阅读全文
0 0
- 状压DP HDU3538 A sample Hamilton path
- HDU3538 A sample Hamilton path
- A sample Hamilton path hdu 3583
- hdu3538
- [agc018d]Tree and Hamilton Path
- hdu3538(概率dp求期望)
- hdoj5492Find a path【dp】
- hdu5492Find a path+DP
- hdu5492Find a path dp
- Hamilton
- hdu5492 Find a path dp
- hdu5492 Find a path(DP)
- HDU5492 Find a path DP
- HDU5492 Find a path【DP】
- AGC018:Tree and Hamilton Path(dfs & 树 哈密尔顿回路)
- hdu 5492 Find a path (DP)
- [dp] hdu 5492 Find a path
- HDU5492 Find a path (DP+水题)
- 动态规划入门 COGS1398 最长上升子序列
- 动态规划入门 P1115 最大子段和(链状)
- 动态规划入门 TYVJ 1305 最大子段和(环状)
- KMP kmp模板
- 关于spring的事务面试题
- 状压DP HDU3538 A sample Hamilton path
- 树形DP Tyvj1052 没有上司的舞会
- 动态规划入门 洛谷P1020 拦截导弹
- 状压DP SCOI2005 互不侵犯King
- 动态规划入门 合并石子 COGS1660 石子合并
- 区间DP code[vs]1258 关路灯
- 动态规划背包问题 洛谷P1064 金明的预算方案
- 树形DP 洛谷P1272 道路重建
- 动态规划入门 洛谷P2409 Y的积木