状压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,bThe 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
 2 #include
 3 #include
 4 #include
 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)
21             for(int j=0;j"%d",&dis[i][j]);
22         for(int i=1;i<=m;i++){
23             scanf("%d%d",&x,&y);
24             f[y]|=(1<//观察了样例才发现 原来y可以多次出现 
25         }
26         for(int i=0;i<(1<)
27             for(int j=0;jMAX;
28         dp[1][0]=0;
29         for(int k=0;k<(1<)
30             for(int i=0;i)
31                 if(dp[k][i]!=MAX)
32                     for(int j=0;j){
33                         if((dis[i][j]==-1)||(!(k&(1<1<continue;//判断是否遍历过 
34                         dp[k|(1<1<//f[k][i]->f[k+{j}][j] + dis(i, j) 
35                     }
36         ans=MAX;
37         for(int i=0;i1<1][i]);
38         if(ans==MAX) printf("-1\n");
39         else printf("%d\n",ans);
40     }
41     return 0;
42 } 

 

转载于:https://www.cnblogs.com/sdfzxh/p/6790796.html

你可能感兴趣的:(java)