状压DP HDU3538 A sample Hamilton path

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.

Output

For each test case,output the shorest length of the hamilton path.
If you could not find a path, output -1

Sample Input

3 0
-1 2 4
-1 -1 2
1 3 -1
4 3
-1 2 -1 1
2 -1 2 1
4 3 -1 1
3 2 3 -1
1 3
0 1
2 3

Sample Output

4
5

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 } 
时间: 2024-10-09 19:13:25

状压DP HDU3538 A sample Hamilton path的相关文章

HDU3538 A sample Hamilton path

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 503    Accepted Submission(s): 200 Problem Description Give you a Graph,you have to start at the city with ID zero. Input The first line is n(1<=n

【滚动数组】【状压dp】Gym - 100956F - Colored Path

f(i,j,S)表示到(i,j),且经由的路径上的颜色集合为S的价值的最小值,从上方和左方转移过来即可. 要注意,内存不足,需要滚动数组优化,即使用了map,还是需要. 路径输出的时候,可以再跑一遍dp,这样就不用再开一个大数组了. 我的写法比较菜.卡了常数 #include<cstdio> #include<algorithm> #include<map> using namespace std; int w[401][401],c[401][401]; typede

P1171 售货员的难题 - 状压DP【最短Hamilton路径】

P1171 售货员的难题 Sol: 最短Hamilton路径,经典的NPC问题,小数据可以通过状压DP 实现. 状态:\(f[i][j]\)表示当前在第i号点,且已经过的点的状态为j 时的最短Hamilton路径. 阶段:若以点为阶段,由于会从点i转移到点i+1,还可能从i+1转移到i-1,不具有无后效性,因此我们考虑以二进制状态为阶段进行转移. 决策:考虑由哪一个点转移而来. 转移:\(f[i][j]=\max \limits_{i\&(1<<i)\&\&i\&

POJ 2288 Islands and Bridges(状压dp)

Language: Default Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 9312   Accepted: 2424 Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the b

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

HDUOJ Clear All of Them I 状压DP

Clear All of Them I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others)Total Submission(s): 1455    Accepted Submission(s): 484 Problem Description Acmers have been the Earth Protector against the evil enemy for a lon

poj 2411 Mondriaan&#39;s Dream(状压DP)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12232   Accepted: 7142 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU5816 Hearthstone(状压DP)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collectible card game from Blizzard Entertainment. Strategies and luck are the most important factors in this game. When you suffer a desperate situation an

BZOJ 1087: [SCOI2005]互不侵犯King( 状压dp )

简单的状压dp... dp( x , h , s ) 表示当前第 x 行 , 用了 h 个 king , 当前行的状态为 s . 考虑转移 : dp( x , h , s ) = ∑ dp( x - 1 , h - cnt_1( s ) , s' ) ( s and s' 两行不冲突 , cnt_1( s ) 表示 s 状态用了多少个 king ) 我有各种预处理所以 code 的方程和这有点不一样 ------------------------------------------------