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<=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,详解见代码

 1 /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 const int INF=1e6;
 9 const int mxn=4194304;//2^22
10 int dp[mxn][22];//[遍历状态][最后到达点]=最短路径
11 int dis[22][22];
12 int pre[22];//每个点的前驱要求
13 int n,m;
14 int xn;
15 int main(){
16     while(scanf("%d%d",&n,&m)!=EOF){
17         memset(pre,0,sizeof pre);
18         int i,j;
19         xn=1<<n;
20         for(i=1;i<xn;i++)
21          for(j=0;j<n;j++){
22              dp[i][j]=INF;
23          }
24         //init
25         for(i=0;i<n;i++)
26          for(j=0;j<n;j++){
27              scanf("%d",&dis[i][j]);
28              if(dis[i][j]==-1)dis[i][j]=INF;
29          }
30         int u,v;
31         for(i=1;i<=m;i++){//保存前驱要求
32             scanf("%d%d",&u,&v);
33             pre[v]|=(1<<u);
34         }
35         dp[1][0]=0;
36         for(i=1;i<xn;i++){
37             for(j=0;j<n;j++){
38                 if(dp[i][j]==INF)continue;//i状态之前没走到
39                 for(int k=1;k<n;k++){
40                     if(!(i&(1<<j)))continue;//j不在已走过的集合中
41                     if(i&(1<<k))continue;//k在走过的集合中
42                     if(pre[k]!=(i&pre[k]))continue;//k点前驱要求未满足
43                     dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dis[j][k]);
44                 }
45             }
46         }
47         int ans=INF;
48         for(i=0;i<n;i++)ans=min(ans,dp[xn-1][i]);
49         if(ans>=INF) printf("-1\n");
50         else printf("%d\n",ans);
51     }
52     return 0;
53 }
时间: 2024-08-07 09:28:46

HDU3538 A sample Hamilton path的相关文章

状压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 T

AGC018D - Tree and Hamilton Path

题意 给出一个n个点的带边权的树,再给出一个n个点的完全图,其中每两个点之间的距离为这两个点在树上的距离,求最大的哈密顿图. 做法 直接考虑在树上的游历,如果存在一条边把树分成大小相同的两半,然后在两半中的点中交替走,这样子显然是最优的,因为每条边都会达到可能的最多的访问次数:否则必然存在一个点(重心),去除这个点之后的森林里每棵树的大小都不大于n/2,这样最优的游历一定是从每棵树出来走到另一棵中没走过的点,这样的安排总是存在的.考虑到最后并不会再访问重心,取重心连出去的权值最小的一条边来承担这

POJ2288 Islands and Bridges

Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that it visits each island exactly once. On our map, there is also a positive integer value associated with e

HDU 1668 Islands and Bridges

Islands and Bridges Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 166864-bit integer IO format: %I64d      Java class name: Main Given a map of islands and bridges that connect these islands, a Hamilton pat

【汉密尔顿、DP|状态压缩】POJ-2288 Islands and Bridges

Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K       Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that it visits each island exactly once.

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

POJ2288Islands and Bridges(状态压缩DP,求最大路和走条数)

Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 8845   Accepted: 2296 Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that i

POJ2288:Islands and Bridges(状态压缩)

Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that it visits each island exactly once. On our map, there is also a positive integer value associated with e

转:转一个搞ACM需要的掌握的算法. .

要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来.  适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什么就眼红,  发挥自己的长处,这才是重要的. 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,  因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打  出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)